This project has moved and is read-only. For the latest updates, please go here.

Exporting sample from soundfont

Oct 3, 2012 at 4:05 PM

Hi,

I'm trying to use NAudio to export samples from a SoundFont file. Here is the code I used :

WaveFormat format = new WaveFormat((int)sampleRate, 16, 1);
WaveFileWriter fileWriter = new WaveFileWriter(path + "/" + sampleName + ".wav", format);
fileWriter.Write(soundFont.SampleData, generator.SampleHeader.start, (int)(generator.SampleHeader.end - generator.SampleHeader.start));
fileWriter.Flush();
fileWriter.Close();

The generator object is a SampleID generator from the Soundfont object.

The wave file is generated but when I play it in VLC, the sound is not correct. It's too short and sounds awfull. Maybe my mistake come from the format configuration, but I have no idea how to solve this.

It would help me a lot if someone could help me with this...

Thanks

Oct 3, 2012 at 8:23 PM

Hi,

It seems like I found the answer to my problem. After looking at the file contents with an hexadecimal editor, I realized that the file was 50% too short. Seems like the Start, End, LoopStart and LoopEnd value of a Sample Header are devided by 2... I have no idea why, and I would be glad if someone could explain me.

Anyway, this code is working :

uint len = offsetEnd - offsetStart;
WaveFormat format = new WaveFormat((int)sampleRate, 16, 1);
WaveFileWriter fileWriter = new WaveFileWriter(path, format);
fileWriter.Write(soundFont.SampleData, checked((int)offsetStart)*2, checked((int) len)*2);
fileWriter.Flush();
fileWriter.Close();

 

 

 

Oct 7, 2012 at 7:51 AM

It is because the sample data is a byte array, whilst the offsets are measured in samples, which are 16 bits, so take two bytes each.