c# - using bass library to decode mp3 file -
how can convert mp3 file custom sample data using bass library in c# ? custom means want result in properties example mono , 5000hz! try code before not works!
public float[] readmonofromfile(string filename, int samplerate, int milliseconds, int startmillisecond) {
int totalmilliseconds = milliseconds <= 0 ? int32.maxvalue : milliseconds + startmillisecond; float[] data = null; //create streams re-sampling int stream = bass.bass_streamcreatefile(filename, 0, 0, bassflag.bass_stream_decode | bassflag.bass_sample_mono | bassflag.bass_sample_float); //decode stream if (stream == 0) throw new exception(bass.bass_errorgetcode().tostring()); int mixerstream = bassmix.bass_mixer_streamcreate(samplerate, 1, bassflag.bass_stream_decode | bassflag.bass_sample_mono | bassflag.bass_sample_float); if (mixerstream == 0) throw new exception(bass.bass_errorgetcode().tostring()); if (bassmix.bass_mixer_streamaddchannel(mixerstream, stream, bassflag.bass_mixer_filter)) { int buffersize = samplerate * 10 * 4; /*read 10 seconds @ each iteration*/ float[] buffer = new float[buffersize]; list<float[]> chunks = new list<float[]>(); int size = 0; while ((float) (size)/samplerate*1000 < totalmilliseconds) { //get re-sampled/mono data int bytesread = bass.bass_channelgetdata(mixerstream, buffer, buffersize); if (bytesread == 0) break; float[] chunk = new float[bytesread/4]; //each float contains 4 bytes array.copy(buffer, chunk, bytesread/4); chunks.add(chunk); size += bytesread/4; //size of data } if ((float) (size)/samplerate*1000 < (milliseconds + startmillisecond)) return null; /*not enough samples return requested data*/ int start = (int) ((float) startmillisecond*samplerate/1000); int end = (milliseconds <= 0) ? size : (int) ((float) (startmillisecond + milliseconds)*samplerate/1000); data = new float[size]; int index = 0; /*concatenate*/ foreach (float[] chunk in chunks) { array.copy(chunk, 0, data, index, chunk.length); index += chunk.length; } /*select specific part of song*/ if (start != 0 || end != size) { float[] temp = new float[end - start]; array.copy(data, start, temp, 0, end - start); data = temp; } } else throw new exception(bass.bass_errorgetcode().tostring()); return data; }
i called function this:
float[] pcm = readmonofromfile(path.getfullpath("1.mp3"), 0, 0, 0);
but in throws exception :the type initializer 'un4seen.bass.bass' threw exception.
add bass.dll bin folder.
Comments
Post a Comment