Ambiera Forum

Discussions, Help and Support.

Ambiera Forum > irrKlang
Saving recorded audio to a WAV file

it2051229
Registered User
Quote
2010-02-19 02:50:45

Hi i've just discovered IrrKlang few days ago and i'm impressed on the stuff that it could do.. I am hoping you could help me with this...
I tried to record sound using the IAudioRecorder so I was able to to get the AudioRecordedBytes.. I'm able to play it if i'm going to add it on the sound Engine as a PCM Data.. I tried to write the bytes on a file which has a header of a WAV file then appended the AudioRecordedBytes.. I tried playing the saved WAV file in windows media player and it doesn't work.. it does play, but it's not the recorded data but just some 0.5 second noise... What am i doing wrong?...


niko
Moderator
Quote
2010-02-19 10:50:11

Take a look at the C++ example on how to write a wave file from the recorded audio. It calls:

// write the recorded audio as wave file
writeWaveFile("recorded.wav", recorder->getAudioFormat(), recorder->getRecordedAudioData());


with the following implementation

// writes the recorded audio data into a .WAV file
void writeWaveFile(const char* filename, SAudioStreamFormat format, void* data)
{
if (!data)
{
printf("Could not save recorded data to %s, nothing recorded\n", filename);
return;
}

FILE* file = fopen(filename, "wb");

if (file)
{
// write wave header
unsigned short formatType = 1;
unsigned short numChannels = format.ChannelCount;
unsigned long sampleRate = format.SampleRate;
unsigned short bitsPerChannel = format.getSampleSize() * 8;
unsigned short bytesPerSample = format.getFrameSize() ;
unsigned long bytesPerSecond = format.getBytesPerSecond();
unsigned long dataLen = format.getSampleDataSize();

const int fmtChunkLen = 16;
const int waveHeaderLen = 4 + 8 + fmtChunkLen + 8;

unsigned long totalLen = waveHeaderLen + dataLen;

fwrite("RIFF", 4, 1, file);
fwrite(&totalLen, 4, 1, file);
fwrite("WAVE", 4, 1, file);
fwrite("fmt ", 4, 1, file);
fwrite(&fmtChunkLen, 4, 1, file);
fwrite(&formatType, 2, 1, file);
fwrite(&numChannels, 2, 1, file);
fwrite(&sampleRate, 4, 1, file);
fwrite(&bytesPerSecond, 4, 1, file);
fwrite(&bytesPerSample, 2, 1, file);
fwrite(&bitsPerChannel, 2, 1, file);

// write data

fwrite("data", 4, 1, file);
fwrite(&dataLen, 4, 1, file);
fwrite(data, dataLen, 1, file);

// finish

printf("Saved audio as %s\n", filename);
fclose(file);
}
else
printf("Could not open %s to write audio data\n", filename);
}


I guess it should be easy to port this to CSharp (I think you are using this?), or at least read the code and find out if you forgot something.


cnmz
Guest
Quote
2012-12-08 18:13:11

That really helps.
Thanks :))


taurian
Registered User
Quote
2016-06-28 15:17:57

Here is what I think the .net version is. However the saved .wav does not play:

        private void SaveWave(string path, AudioFormat format, byte[] waveData)
{
//Play the audio for testing purposes
ss = engine0.AddSoundSourceFromPCMData(waveData, "sound", format);
engine0.Play2D(ss, true, false, false);

// write wave header
ushort formatType = 1;
ushort numChannels = (ushort)format.ChannelCount;
ulong sampleRate = (ulong)format.SampleRate;
ushort bitsPerChannel = (ushort)(format.SampleSize * 8);
ushort bytesPerSample = (ushort)format.FrameSize;
ulong bytesPerSecond = (ulong)format.BytesPerSecond;
ulong dataLen = (ulong)format.SampleDataSize;

const int fmtChunkLen = 16;
const int waveHeaderLen = 4 + 8 + fmtChunkLen + 8;

ulong totalLen = waveHeaderLen + dataLen;

FileStream fs = new FileStream(path, FileMode.Create, FileAccess.Write);
BinaryWriter bw = new BinaryWriter(fs);
byte[] riff = System.Text.Encoding.ASCII.GetBytes("RIFF");
bw.Write(riff);
bw.Write(totalLen);
byte[] fmt = System.Text.Encoding.ASCII.GetBytes("WAVEfmt ");
bw.Write(fmt);
bw.Write(fmtChunkLen);
bw.Write(formatType);
bw.Write(numChannels);
bw.Write(sampleRate);
bw.Write(bytesPerSecond);
bw.Write(bytesPerSample);
bw.Write(bitsPerChannel);
byte[] data = System.Text.Encoding.ASCII.GetBytes("data");
bw.Write(data);
bw.Write(waveData.Length);
bw.Write(waveData);
bw.Close();
fs.Close();
}



Create reply:


Posted by: (you are not logged in)


Enter the missing letter in: "Internation?l" (you are not logged in)


Text:

 

  

Possible Codes


Feature Code
Link [url] www.example.com [/url]
Bold [b]bold text[/b]
Image [img]http://www.example.com/image.jpg[/img]
Quote [quote]quoted text[/quote]
Code [code]source code[/code]

Emoticons


   






Copyright© Ambiera e.U. all rights reserved.
Privacy Policy | Terms and Conditions | Imprint | Contact