-
Save/record sound
Hi, I have serch perty much everywhere on this and didnt finde much at all. I can get sounds/music (.wavs) to play but I cant seem to finde much on saving them. I want to also save them from a microphone like the little program on windows "sound recorder". I may also have this in the wrong place, if I do I am sorry. I am trying to use C++ on this tho.
-
-
Thanks, I did look there but I didnt see what you just found. I will take a look at it.
-
Otherwise, if you want some blunt C code, i've got some for you below... You can choose whether to record or playback...
However, it saves raw data! This means that it is HUGE! You will need to 'wavify' it before reading/writing wav files, but most of the useful (recording from mic) code is there...
Btw, I don't know how good the code is, since I wrote it quickly one night, and may contain awful bugs! But it works here, so...
Code:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <windows.h>
#include <mmsystem.h>
#define FREQUENCY 44100
//Length is typically 44100*time...
int StartRecord(char * data,int length)
{
int sampleRate = FREQUENCY;
HWAVEIN hWaveIn;
WAVEHDR WaveInHdr;
WAVEFORMATEX pFormat;
pFormat.wFormatTag=WAVE_FORMAT_PCM; // simple, uncompressed format
pFormat.nChannels=1; // 1=mono, 2=stereo
pFormat.nSamplesPerSec=sampleRate; // 44100
pFormat.nAvgBytesPerSec=sampleRate; // = nSamplesPerSec * n.Channels * wBitsPerSample/8
pFormat.nBlockAlign=1; // = n.Channels * wBitsPerSample/8
pFormat.wBitsPerSample=8; // 16 for high quality, 8 for telephone-grade
pFormat.cbSize=0;
if(waveInOpen(&hWaveIn, WAVE_MAPPER,&pFormat,0L, 0L, WAVE_FORMAT_DIRECT)) return 1;
WaveInHdr.lpData = (LPSTR)data;
WaveInHdr.dwBufferLength = length;
WaveInHdr.dwBytesRecorded=0;
WaveInHdr.dwUser = 0L;
WaveInHdr.dwFlags = 0L;
WaveInHdr.dwLoops = 0L;
waveInPrepareHeader(hWaveIn, &WaveInHdr, sizeof(WAVEHDR));
if(waveInAddBuffer(hWaveIn, &WaveInHdr, sizeof(WAVEHDR))) return 2;
if(waveInStart(hWaveIn)) return 3;
while(waveInUnprepareHeader(hWaveIn,&WaveInHdr,sizeof(WAVEHDR)) == WAVERR_STILLPLAYING) Sleep(1);
waveInClose(hWaveIn);
return 0;
}
int StartPlayback(char * data,int length)
{
int sampleRate = FREQUENCY;
HWAVEOUT hWaveOut;
WAVEHDR WaveOutHdr;
WAVEFORMATEX pFormat;
pFormat.wFormatTag=WAVE_FORMAT_PCM; // simple, uncompressed format
pFormat.nChannels=1; // 1=mono, 2=stereo
pFormat.nSamplesPerSec=sampleRate; // 44100
pFormat.nAvgBytesPerSec=sampleRate; // = nSamplesPerSec * n.Channels * wBitsPerSample/8
pFormat.nBlockAlign=1; // = n.Channels * wBitsPerSample/8
pFormat.wBitsPerSample=8; // 16 for high quality, 8 for telephone-grade
pFormat.cbSize=0;
if(waveOutOpen(&hWaveOut, WAVE_MAPPER,&pFormat,0L, 0L, WAVE_FORMAT_DIRECT)) return 1;
WaveOutHdr.lpData = (LPSTR)data;
WaveOutHdr.dwBufferLength = length;
WaveOutHdr.dwBytesRecorded=0;
WaveOutHdr.dwUser = 0L;
WaveOutHdr.dwFlags = 0L;
WaveOutHdr.dwLoops = 0L;
waveOutPrepareHeader(hWaveOut, &WaveOutHdr, sizeof(WAVEHDR));
if(waveOutWrite(hWaveOut,&WaveOutHdr,sizeof(WAVEHDR))) return 2;
while(waveOutUnprepareHeader(hWaveOut,&WaveOutHdr,sizeof(WAVEHDR)) == WAVERR_STILLPLAYING) Sleep(1);
waveOutClose(hWaveOut);
return 0;
}
int main()
{
int mode=0;
printf("Here we go: *****\nMode: (1 -record / 2 -playback) ");
scanf("%i",&mode);
int size = (FREQUENCY*60);
char *data = malloc(sizeof(char)*size);
if(mode==1) {
printf("Recording...\r");
StartRecord(data,size);
printf("Processing..\r");
//encode wav file from data here!!
printf("Saving ...\r");
FILE *f=fopen("data.raw","w");
fwrite(data,sizeof(char),size,f);
fclose(f);
}
else {
printf("Playing... \r");
FILE *f;
f=fopen("data.raw","r");
fread(data,sizeof(char),size,f);
fclose(f);
//decode wav data here!!
StartPlayback(data,size);
}
free(data);
return 0;
}
Hope this helps!
Franchie.