c# - Manipulating Mp3's as Array Using NAudio -


i'm trying reimplement existing matlab 8-band equalizer gui created project last week in c#. in matlab, songs load dynamic array memory, can freely manipulated , playing easy sound(array).

i found naudio library conveniently has mp3 extractors, players, , both convolution , fft defined. able open mp3 , read data array (though i'm not positive i'm going correctly.) however, after looking through couple of examples, i'm struggling figure out how take array , write stream in such way play (i don't need write file).

following examples found, read mp3's this:

   private byte[] createinputstream(string filename)     {         byte[] stream;         if (filename.endswith(".mp3"))         {             wavestream mp3reader = new mp3filereader(filename);             songformat = mp3reader.waveformat; // songformat class field             long sizeofstream = mp3reader.length;             stream = new byte[sizeofstream];             mp3reader.read(stream, 0, (int) sizeofstream);         }         else         {             throw new invalidoperationexception("unsupported exception");         }         return stream;     } 

now have array of bytes presumably containing raw audio data, intend covert floats run through dsp module. right now, however, i'm trying see if can play array of bytes.

        stream outstream = new memorystream(stream);         wavefilewriter wfr = new wavefilewriter(outstream, songformat);         // outputstream array of bytes , class variable         wfr.write(outputstream, 0, (int)outputstream.length);         wavefilereader wr = new wavefilereader(outstream);         volumestream = new wavechannel32(wr);         waveoutdevice.init(volumestream);         waveoutdevice.play(); 

right i'm getting errors thrown in wavefilereader(outstream) can't read past end of stream. suspect that's not thing i'm not doing correctly. insights?

your code isn't working because never close wavefilewriter headers aren't written correctly, , need rewind memorystream.

however, there no need writing wav file if want play array of byes. use rawsourcewavestream , pass in memorystream.

you may find audiofilereader class more suitable needs provide samples floating point directly, , allow modify volume.


Comments

Popular posts from this blog

jquery - How can I dynamically add a browser tab? -

node.js - Getting the socket id,user id pair of a logged in user(s) -

keyboard - C++ GetAsyncKeyState alternative -