Created
January 31, 2010 01:59
-
-
Save atsushieno/290843 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// It doesn't generate useful file. | |
using System; | |
using System.Collections.Generic; | |
using System.IO; | |
using System.Text; | |
using NRenoiseTools; | |
using NAudio.SoundFont; | |
using NAudio.Wave; | |
using SInstrument = NAudio.SoundFont.Instrument; | |
using XInstrument = NRenoiseTools.Instrument; | |
using SSampleHeader = NAudio.SoundFont.SampleHeader; | |
using XSample = NRenoiseTools.Sample; | |
namespace Commons.Music.Sf2Xrni | |
{ | |
public class Driver | |
{ | |
public static void Main (string [] args) | |
{ | |
var sf2xrni = new Sf2Xrni (); | |
foreach (var file in args) | |
sf2xrni.Convert (file, Path.ChangeExtension (file, ".xrni")); | |
} | |
} | |
public class Sf2Xrni | |
{ | |
public void Convert (string soundFontFile, string xrniFile) | |
{ | |
var sf2 = new SoundFont (soundFontFile); | |
var xrni = new XInstrument (); | |
// do conversion | |
var samples = new List<XSample> (); | |
foreach (var sh in sf2.SampleHeaders) | |
samples.Add (ConvertSample (sh, sf2.SampleData)); | |
xrni.Name = sf2.FileInfo.BankName; | |
xrni.Samples = samples.ToArray (); | |
xrni.Save (xrniFile); | |
} | |
XSample ConvertSample (SSampleHeader sh, byte [] sample) | |
{ | |
var xs = new XSample (); | |
xs.LoopStart = sh.StartLoop - sh.Start; | |
xs.LoopEnd = sh.EndLoop - sh.End; | |
xs.Name = sh.SampleName; | |
xs.FileName = xs.Name + ".wav"; | |
var ms = new MemoryStream (); | |
var wfw = new WaveFileWriter (ms, new WaveFormat ()); | |
wfw.WriteData (sample, (int) sh.Start, (int) (sh.End - sh.Start)); | |
wfw.Close (); | |
xs.Buffer = ms.ToArray (); | |
return xs; | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment