GIDForums  

Go Back   GIDForums > Computer Programming Forums > C++ Forum
User Name
Password
Register FAQ Members List Calendar Search Today's Posts Mark Forums Read

 
 
Thread Tools Search this Thread Rate Thread
  #1  
Old 06-Mar-2009, 21:02
NikhilB NikhilB is offline
New Member
 
Join Date: Mar 2009
Posts: 7
NikhilB is an unknown quantity at this point

Saving a .wav file


Hi,

I am trying to create a CLI to record and save audio to a wav file. I am successful in recording audio but am not able to save the audio.
Here are the files I used. These are open source.


www.codeproject.com

juhara.com( Explaination of code )

Here is my main.cpp code
The second main.cpp directly records the audio to buffer and plays it back. I need to use sleep so that it can record "time" seconds.

Please help me out.

Thanks

Nikhil

CPP / C++ / C Code:
#include <Windows.h>
#include "VoiceRecording.h"
#include "VoiceBase.h"
#include "VoicePlaying.h"
#include "CWaveFile.h"
#include "iostream" //for storing
 #include <stdio.h> //for storing
using namespace std;
 # define time 10
 
typedef struct{
char RIFF[4];
DWORD bytes;
char WAVE[4];
char fmt[4];
int siz_wf;
WORD wFormatTag;
WORD nChannels;
DWORD nSamplesPerSec;
DWORD nAvgBytesPerSec;
WORD nBlockAlign;
WORD wBitsPerSample;
char data[4];
DWORD pcmbytes;
} WAVHEADER;




int main ()
{
CVoiceRecording m_Record;
CVoicePlaying m_Play;
int blocksize;
DWORD bWrite; 
m_Record.PrepareBuffer(time);    //prepare buffer for recording "time" seconds.

m_Record.Open();
cout<<" Recording "<<endl;
m_Play.PrepareBuffer(time);    //prepare buffer for playing of "time" seconds of data
m_Play.Open();    
cout<<" Playing back "<<endl;
if (m_Record.IsOpen())
{
    m_Record.Record();
	 Sleep(time*1000);
}

//after finishing the record scenario,

//play the buffer, first copy recorded buffer to m_Play buffer
m_Play.CopyBuffer(m_Record.buffer, time);

if (m_Play.IsOpen())
{
    m_Play.Play();
	Sleep(time*1000);
}


// code that use to save
blocksize=sizeof(m_Record.buffer);
WAVHEADER wh;
memcpy(wh.RIFF,"RIFF",4);
wh.bytes = blocksize+36;
memcpy(wh.WAVE,"WAVE",4);
memcpy(wh.fmt,"fmt ",4);
wh.siz_wf=16;
memcpy(wh.data,"data",4);
wh.pcmbytes = blocksize;
wh.wFormatTag =1;
wh.nChannels = 1;
wh.nSamplesPerSec = 8000;
wh.nAvgBytesPerSec = 8000;
wh.nBlockAlign = 1;
wh.wBitsPerSample = 16;

HANDLE hFile = CreateFile("audio1.wav", // file to create
GENERIC_WRITE, // open for writing
0, // do not share
NULL, // default security
CREATE_ALWAYS, // overwrite existing
FILE_ATTRIBUTE_NORMAL, // normal file
NULL);

WriteFile(hFile,(char*)&wh,sizeof(WAVHEADER),&bWrite,NULL);
WriteFile(hFile,m_Record.buffer,blocksize,&bWrite,NULL);
CloseHandle(hFile);

}

CPP / C++ / C Code:
#include <Windows.h>
#include "VoiceRecording.h"
#include "VoiceBase.h"
#include "VoicePlaying.h"
#include "CWaveFile.h"
#include "iostream" //for storing
 #include <stdio.h> //for storing
using namespace std;
 # define time 10
int main ()
{
CVoiceRecording m_Record;
CVoicePlaying m_Play;
 m_Record.PrepareBuffer(time);    //prepare buffer for recording "time" seconds.

m_Record.Open();
cout<<" Recording "<<endl;
m_Play.PrepareBuffer(time);    //prepare buffer for playing of "time" seconds of data

m_Play.Open();    
cout<<" Playing back "<<endl;
if (m_Record.IsOpen())
{
    m_Record.Record();
	 Sleep(time*1000);
}

//after finishing the record scenario,

//play the buffer, first copy recorded buffer to m_Play buffer
m_Play.CopyBuffer(m_Record.buffer, time);

if (m_Play.IsOpen())
{
    m_Play.Play();
	Sleep(time*1000);
}


  #2  
Old 07-Mar-2009, 05:09
Mexican Bob's Avatar
Mexican Bob Mexican Bob is offline
Member
 
Join Date: Mar 2008
Location: Chicxulub, Yucatán
Posts: 226
Mexican Bob is a jewel in the roughMexican Bob is a jewel in the roughMexican Bob is a jewel in the rough

Re: Help with saving a wav file**Urgent**


Is CreateFile returning a valid file handle? I don't suppose that you would ever know, since you're not checking it.

If this is so urgent, I'd recommend that you at least ASK ONE QUESTION. Something other than the implied "need for help." Imagine that you are dialing an automated 911 service (to receive "urgent assistance"). The voice at the other end of the line says: "911 state your emergency"

That's what this forum does. We're waiting to hear from those who have problems. How you present your problem is important. What you've done is provided an information dump and a very ambiguous statement of your emergency. You apparently are not "saving a wav file." We don't even know if your code compiles cleanly or if you have any runtime exceptions.

We don't even know WHAT the urgency is. Is it a matter of a few minutes, hours, days? Do you even have time to wait for the ambulance to arrive? If you believe that you need an ambulance, perhaps you should at least give clear directions as to WHERE YOU ARE so that the ambulance can find you!



MxB
  #3  
Old 07-Mar-2009, 11:44
NikhilB NikhilB is offline
New Member
 
Join Date: Mar 2009
Posts: 7
NikhilB is an unknown quantity at this point

Re: Help with saving a wav file**Urgent**


Thanks Mexican Bob.

Create File is able to create a new file and load the header into it. I do not know if its returning a valid handle.

I do know that my statement of urgency was vague. I think I do not have to explain myself to everyone in this forum. I just want people to know that its a little urgent. And if they know anything about what I am having a problem, let me know.That's all I have to communicate. You seem to have good communication skills. Keep it up.
I actually attached two links where you could download my project. They are not working. I am sorry about that. I did not check.

rapidshare.com

rapidshare.com/files/206200528/VoiceRecording.rar.htm

As I have told you before, I am able to record and play back audio. I am not able to save it to a file. NO ERRORS. Its not saving. Thats all.

Do not worry about my urgency, if you can, help me out.

I placed WriteFile in VoicePlaying.cpp file

Thanks anyways

Nikhil
  #4  
Old 07-Mar-2009, 13:43
davekw7x davekw7x is offline
Outstanding Member
 
Join Date: Feb 2004
Location: Left Coast, USA
Posts: 5,218
davekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to behold

Re: Help with saving a wav file**Urgent**


Quote:
Originally Posted by NikhilB
... I am not able to save it to a file...

In main.cpp:
CPP / C++ / C Code:
CVoiceRecording m_Record;
.
.
.
blocksize=sizeof(m_Record.buffer);
.
.
.
wh.pcmbytes = blocksize;
.
.
.
WriteFile(hFile,(char*)&wh,sizeof(WAVHEADER),&bWrite,NULL);
WriteFile(hFile,m_Record.buffer,blocksize,&bWrite,NULL);
.
.
.

In VoiceRecording.h:
CPP / C++ / C Code:
class CVoiceRecording : public CVoiceBase  
{
.
.
.};

In VoiceBase.h:
CPP / C++ / C Code:
class CVoiceBase  
{
.
.
.
	char* buffer;.
.
.

Bottom line: After the header, the number of bytes that it writes is equal to the size of a pointer to char (probably 4 unless you are compiling with a 64-bit compiler on a 64-bit operating system, in which case it will probably be 8).

The size of the header is 44 bytes, right? What is the size of audio.wav after the program finishes?

Regards,

Dave
Last edited by davekw7x : 07-Mar-2009 at 14:16.
  #5  
Old 07-Mar-2009, 14:14
NikhilB NikhilB is offline
New Member
 
Join Date: Mar 2009
Posts: 7
NikhilB is an unknown quantity at this point

Re: Help with saving a wav file**Urgent**


Hi Dave,

Thank you for your reply. I have been working on that buffer since yesterday.

I have changed a part of the code.

rapidshare.com/files/206565930/Voice.rar.html

I have put the WriteFile code in VoicePlaying.cpp. I assume that the buffer is cleared after it plays and as such I wanted to access that buffer before its cleared.

I get no errors. I recorded for 60 seconds and it gave me a file size of 937KB. I thought it was recording but when I try to play it, its not working. I think my header is corrupted. I am currently working to get the header right.


img9.imageshack.us/img9/9392/72813742.jpg

I opened the wav file using HEXEDIT and am not sure of the header there. The other set of bytes show there is some data ( I do not know how to read wav files) chmaas.handshake.de/delphi/freeware/xvi32/xvi32.htm#download ( place www. before the link)

Thanks and let me know

Nikhil

P.S. I just used a hex editor and changed the first 44 bytes of the recorded file to a sample wav file ( downloaded from internet) and I was able to play it.
Its obvious that the header is corrupted.
  #6  
Old 08-Mar-2009, 10:27
davekw7x davekw7x is offline
Outstanding Member
 
Join Date: Feb 2004
Location: Left Coast, USA
Posts: 5,218
davekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to behold

Re: Help with saving a wav file**Urgent**


Quote:
Originally Posted by NikhilB
...
I have changed a part of the code...
I tried to tell you exactly what was wrong with your original program. You had blocksize equal to four in the output file header, and you only wrote four bytes to the data section of the output file.

For single-channel recording:

(number of bits) = (number of seconds) times (samples per second) times (bits per sample)

Your "blocksize" is the number of data bytes, so you divide the number of bits by eight, right?

Maybe you could go back and change the following section in Main.cpp:
CPP / C++ / C Code:
    WAVHEADER wh;
    memcpy(wh.RIFF, "RIFF", 4);
    memcpy(wh.WAVE, "WAVE", 4);
    memcpy(wh.fmt, "fmt ", 4);
    wh.siz_wf          = 16;
    memcpy(wh.data, "data", 4);
    wh.wFormatTag      = 1;
    wh.nChannels       = 1;
    wh.nSamplesPerSec  = 8000;
    wh.nAvgBytesPerSec = 8000;
    wh.nBlockAlign     = 1;
    wh.wBitsPerSample  = 16;
    blocksize          = time * wh.nSamplesPerSec * wh.wBitsPerSample / 8;
    wh.bytes           = blocksize + 36;
    wh.pcmbytes        = blocksize;
    //
    // For debugging print out the block size
    //
    cout << "blocksize = " << blocksize << endl;


Or some such thing.

For ten seconds recording (single channel), blocksize = 160,000 and the file size will be exactly 160,044 bytes.

Regards,

Dave
  #7  
Old 08-Mar-2009, 11:56
NikhilB NikhilB is offline
New Member
 
Join Date: Mar 2009
Posts: 7
NikhilB is an unknown quantity at this point

Re: Help with saving a wav file**Urgent**


Hi Dave,
I changed the below in my VoicePlaying.cpp and it worked. I was supposed to look at the header buffer pointer and size of it.

CPP / C++ / C Code:
	WriteFile(hFile,&wh,sizeof(wh),&bWrite,NULL);

	WriteFile(hFile,WaveHeader.lpData,WaveHeader.dwBufferLength,&bWrite,NULL);

Its working perfectly fine. Thanks a lot !!

Nikhil
  #8  
Old 09-Mar-2009, 18:47
babykwes babykwes is offline
Awaiting Email Confirmation
 
Join Date: Mar 2009
Posts: 2
babykwes is on a distinguished road

Re: Help with saving a wav file**Urgent**


Hi,

I saw your thread and it looked like it could be a life saver.. Currrently i'm doing a project on speech recognition and for a few days now I'm battling trying to sharpen me C++ programming.. I'm now down to the programming part which i'm not too good at and I really need help acquiring the source codes needed for recording and saving a wave file using c++. I see you did the same thing so any help you could render would be GREATLY APPRECIATED!

Gratefully, kwesi
  #9  
Old 11-Mar-2009, 10:40
NikhilB NikhilB is offline
New Member
 
Join Date: Mar 2009
Posts: 7
NikhilB is an unknown quantity at this point

Re: Help with saving a wav file**Urgent**


Hi Kwesi,

Could you give me more information about your project. I mean, do you need a CLI or a GUI of the program. I haven't made an EXE of the code as I need to use it with another program.
Let me know and I shall send you the code

Nikhil
  #10  
Old 12-Mar-2009, 23:44
NikhilB NikhilB is offline
New Member
 
Join Date: Mar 2009
Posts: 7
NikhilB is an unknown quantity at this point

Re: Help with saving a wav file**Urgent**


Hi,

Thank you for your support. Kindly check this thread.

I need help with creating a Win32 console output which can be executed on any computer.
http://www.gidforums.com/showthread.php?p=80877

Thanks

Nikhil
 
 

Recent GIDBlogToyota - 2009 May Promotion by Nihal

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
Saving http responses in a file thisisme C Programming Language 2 09-Sep-2008 21:01
Saving widget graphics silverstar FLTK Forum 3 12-Jun-2007 14:45
Saving 2D array of doubles to text for MS Excel markiebbc MS Visual C++ / MFC Forum 3 20-Aug-2006 15:45
STL troubles saving and finding Clive73 C++ Forum 2 19-May-2005 09:50
saving html text dopee MySQL / PHP Forum 1 17-Jan-2005 05:15

Network Sites: GIDNetwork · GIDWebHosts · GIDSearch · Learning Journal by J de Silva, The

All times are GMT -6. The time now is 19:44.


vBulletin, Copyright © 2000 - 2009, Jelsoft Enterprises Ltd.