public Sf2Synth(Sf2File file, Table table, int voiceCount) { this.file = file; this.table = table; masterGain = 1; this.voiceCount = voiceCount; voices = new Voice[voiceCount]; for (int i = 0; i < voiceCount; i += 1) { voices[i].Init(i, file.data, table); } #if MIDIF_DEBUG_VISUALIZER WaveVisualizer.Request(0, 1024); WaveVisualizer.Request(1, 1024); WaveVisualizer.Request(2, 1024); WaveVisualizer.Request(3, 1024); WaveVisualizer.Request(4, 1024); WaveVisualizer.Request(5, 1024); #endif Reset(); }
public void NoteOn(int channel, byte note, byte velocity) { // if (channel != 10) return; NoteOff(channel, note, 0); if (velocity == 0) { UnityEngine.Debug.LogWarning("Note on with 0 velocity"); return; } var preset = file.presets[channels[channel].presetIndex]; for (int i = 0, endI = preset.presetZones.Length; i < endI; i += 1) { var presetZone = preset.presetZones[i]; if (!presetZone.zone.Contains(note, velocity)) { continue; } var instrument = presetZone.instrument; for (int j = 0, endJ = instrument.instrumentZones.Length; j < endJ; j += 1) { var instrumentZone = instrument.instrumentZones[j]; if (!instrumentZone.zone.Contains(note, velocity)) { continue; } // if (instrumentZone.sampleHeader.sampleName != "Stacked Saw-C6") continue; // Console.Log("synth noteon", note, velocity, "preset", preset.presetName, "instrument", instrument.instName, "sample", instrumentZone.sampleHeader.sampleName); var zone = Sf2File.GetAppliedZone(instrument.globalZone, instrumentZone.zone, preset.globalZone, presetZone.zone); int k = firstFreeVoice; if (k == -1) { // UnityEngine.Debug.LogFormat("Not enough notes active {0} free {1}", CountVoices(firstActiveVoice), CountVoices(firstFreeVoice)); // return; // find the oldest active voice and move it to front for (k = firstActiveVoice; voices[voices[k].next].next != -1; k = voices[k].next) { ; } int next = voices[k].next; voices[k].next = -1; k = next; voices[k].next = firstActiveVoice; firstActiveVoice = k; } else { firstFreeVoice = voices[k].next; voices[k].next = firstActiveVoice; firstActiveVoice = k; } voices[k].note = note; voices[k].velocity = velocity; voices[k].channel = channel; voices[k].On(instrumentZone.sampleHeader, zone); UpdateVoicePitch(k); UpdateVoiceGain(k); } } }