public override void PlayInstrument(uint instrumentNumber = 0) { PlayBackWaveProvider waveStream = new PlayBackWaveProvider(ModuleConst.SOUNDFREQUENCY, ModuleConst.MIX_CHANNELS == ModuleConst.STEREO); XM_Sample sample = ((XM_Instrument)Instruments[(int)instrumentNumber - 1]).sample; string mes = "\n\n\nSample number : " + sample.orderNumber; float period = 7680.0f - (48 + sample.relativeNoteNumber) * 64.0f; float frequency = (float)(8363 * Math.Pow(2, (4608.0f - period) * 0.001302083f)); float periodInc = (period != 0) ? frequency * 2 / ModuleConst.SOUNDFREQUENCY : 1.0f; float pos = 0; mes += " freq : " + period; DebugMes(mes); while (pos < sample.length * 4 - 1) { waveStream.Write((short)sample[(int)pos]); pos += periodInc; } WaveOutEvent waveOut = new WaveOutEvent(); waveOut.Init(waveStream); waveOut.Play(); }
public void ReadFromStream(Stream stream, ref uint sampleOrder) { long startInstrumentPosition = stream.Position; instrumentSize = ModuleUtils.ReadDWord(stream); name = ModuleUtils.ReadString0(stream, 22); instrumentType = ModuleUtils.ReadByte(stream); // Length samplesNumber = ModuleUtils.ReadWord(stream); samples.Clear(); if (samplesNumber > 0) { headerSize = ModuleUtils.ReadDWord(stream); for (uint i = 0; i < 96; i++) { keymapAssignements.Add(ModuleUtils.ReadByte(stream)); } for (uint i = 0; i < 24; i++) { pointsForVolumeEnvelope.Add(ModuleUtils.ReadWord(stream)); } for (uint i = 0; i < 24; i++) { pointsForPanningEnvelope.Add(ModuleUtils.ReadWord(stream)); } numberOfVolumePoints = ModuleUtils.ReadByte(stream); numberOfPanningPoints = ModuleUtils.ReadByte(stream); volumeSustainPoint = ModuleUtils.ReadByte(stream); volumeLoopStartPoint = ModuleUtils.ReadByte(stream); volumeLoopEndPoint = ModuleUtils.ReadByte(stream); panningSustainPoint = ModuleUtils.ReadByte(stream); panningLoopStartPoint = ModuleUtils.ReadByte(stream); panningLoopEndPoint = ModuleUtils.ReadByte(stream); volumeType = ModuleUtils.ReadByte(stream); // bit 0: On; 1: Sustain; 2: Loop; panningType = ModuleUtils.ReadByte(stream); // bit 0: On; 1: Sustain; 2: Loop; vibratoType = ModuleUtils.ReadByte(stream); vibratoSweep = ModuleUtils.ReadByte(stream); vibratoDepth = ModuleUtils.ReadByte(stream); vibratoRate = ModuleUtils.ReadByte(stream); volumeFadeout = ModuleUtils.ReadWord(stream); //for (uint i = 0; i < 22; i++) reserved.Add(ModuleUtils.ReadByte(stream)); //System.Diagnostics.Debug.WriteLine("Seek : " + stream.Position + " " + (instrumentSize - 243) + "\n"); stream.Seek(instrumentSize - 241, SeekOrigin.Current); long sampleDataLength = 0; for (uint i = 0; i < samplesNumber; i++) { var sample = new XM_Sample(); sample.readHeaderFromStream(stream); sample.orderNumber = sampleOrder++; samples.Add(sample); sampleDataLength += sample.sizeInBytes; } long startSampleDataPosition = stream.Position; foreach (XM_Sample sample in samples) { sample.readSampleDataFromStream(stream); } stream.Seek(startSampleDataPosition + sampleDataLength, SeekOrigin.Begin); } else { stream.Seek(startInstrumentPosition + instrumentSize, SeekOrigin.Begin); } sample = (samples.Count > 0) ? samples[0] : null; if (sample != null) { volume = sample.volume; } }