c# - How to convert PCM data to wav file? -
i have data in rawdata1 aray short array. have converted byte aray.then created wav file header , header , byte array data written in file. getting error "windows media player getting problem while playing file".can body me?
public void save file { byte[] byte_array = new byte[rawdata1.length * 2]; (int = 0; < rawdata1.length; ++i) { //byte_array[2 * i] = getbyte1(rawdata1[i]); //byte_array[2 * + 1] = getbyte2(rawdata1[i]); byte_array[2 * i] = getbyte2(rawdata1[i]); byte_array[2 * + 1] = getbyte1(rawdata1[i]); } uint numsamples = 44100; ushort numchannels = 2; ushort samplelength = 1; // in bytes uint samplerate = 22050; storagefolder storagefolder = knownfolders.musiclibrary; storagefile file = await storagefolder.createfileasync("sample.wav", creationcollisionoption.generateuniquename); stream stream = await file.openstreamforwriteasync(); binarywriter wr = new binarywriter(stream); wr.write("riff".tochararray()); wr.write(36 + numsamples * numchannels * samplelength); wr.write("wavefmt".tochararray()); wr.write(16); wr.write((ushort)1); wr.write(numchannels); wr.write(samplerate); wr.write(samplerate * samplelength * numchannels); wr.write(samplelength * numchannels); wr.write((ushort)(8 * samplelength)); wr.write("data".tochararray()); wr.write(numsamples * samplelength); wr.write(byte_array); wr.dispose(); stream.dispose(); } public static byte getbyte1(short s) { return (byte)s; } public static byte getbyte2(short s) { int temp= s >> 8; return (byte)temp; }
can me?
here detail description of wav format
the canonical wave format starts riff header: ------------------------------------------------- offset size name description ------------------------------------------------- 0 4 chunkid contains letters "riff" in ascii form (0x52494646 big-endian form). 4 4 chunksize 36 + subchunk2size, or more precisely: 4 + (8 + subchunk1size) + (8 + subchunk2size) size of rest of chunk following number. size of entire file in bytes minus 8 bytes 2 fields not included in count: chunkid , chunksize. 8 4 format contains letters "wave" (0x57415645 big-endian form). "wave" format consists of 2 subchunks: "fmt " , "data": "fmt " subchunk describes sound data's format: 12 4 subchunk1id contains letters "fmt " (0x666d7420 big-endian form). 16 4 subchunk1size 16 pcm. size of rest of subchunk follows number. 20 2 audioformat pcm = 1 (i.e. linear quantization) values other 1 indicate form of compression. 22 2 numchannels mono = 1, stereo = 2, etc. 24 4 samplerate 8000, 44100, etc. 28 4 byterate == samplerate * numchannels * bitspersample/8 32 2 blockalign == numchannels * bitspersample/8 number of bytes 1 sample including channels. wonder happens when number isn't integer? 34 2 bitspersample 8 bits = 8, 16 bits = 16, etc. 2 extraparamsize if pcm, doesn't exist x extraparams space parameters "data" subchunk contains size of data , actual sound: 36 4 subchunk2id contains letters "data" (0x64617461 big-endian form). 40 4 subchunk2size == numsamples * numchannels * bitspersample/8 number of bytes in data. can think of size of read of subchunk following number. 44 * data actual sound data.
all details here
Comments
Post a Comment