Save/record sound

This is a discussion on Save/record sound within the Windows Programming forums, part of the Platform Specific Boards category; Hi, I have serch perty much everywhere on this and didnt finde much at all. I can get sounds/music (.wavs) ...

  1. #1
    adr
    adr is offline
    Registered User
    Join Date
    Dec 2005
    Posts
    155

    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.

  2. #2
    train spotter
    Join Date
    Aug 2001
    Location
    near a computer
    Posts
    3,855
    "Man alone suffers so excruciatingly in the world that he was compelled to invent laughter."
    Friedrich Nietzsche

    "I spent a lot of my money on booze, birds and fast cars......the rest I squandered."
    George Best

    "If you are going through hell....keep going."
    Winston Churchill

  3. #3
    adr
    adr is offline
    Registered User
    Join Date
    Dec 2005
    Posts
    155
    Thanks, I did look there but I didnt see what you just found. I will take a look at it.

  4. #4
    Registered User
    Join Date
    May 2004
    Posts
    6
    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.
    Last edited by Franchie; 02-16-2006 at 02:57 PM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Sound lags in multi-thread version
    By VirtualAce in forum Game Programming
    Replies: 23
    Last Post: 08-27-2008, 11:54 AM
  2. Low latency sound effects
    By VirtualAce in forum Game Programming
    Replies: 0
    Last Post: 12-21-2004, 12:58 AM
  3. Updated sound engine code
    By VirtualAce in forum Game Programming
    Replies: 8
    Last Post: 11-18-2004, 11:38 AM
  4. DirectSound - multiple sounds
    By Magos in forum Game Programming
    Replies: 9
    Last Post: 03-03-2004, 03:33 PM
  5. sounds?
    By BODYBUILDNERD in forum C++ Programming
    Replies: 6
    Last Post: 12-06-2002, 02:34 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21