void OnDisable() { // <-- disconnect mixer mix.Terminate(); // <-- terminate thread mix = null; }
void Awake() { //UnityEngine.Debug.Log("Awake"); mixer = new MyMixer((uint)AudioSettings.outputSampleRate, false, mixingBufferLength, baseFrequency); mixer.TickCallback = () => { tick(); }; synthesizers = new List <MySynthesizer>(); synthesizers.Add(new MySynthesizerPM8(mixer, numVoices)); synthesizers.Add(new MySynthesizerSS8(mixer, numVoices)); synthesizers.Add(new MySynthesizerCT8(mixer, numVoices)); sequencers = new List <MyMMLSequencer>(); acgState.mixer = new MyMixer(AudioClipGeneratorState.frequency, true, mixingBufferLength, baseFrequency); acgState.sequencer = new MyMMLSequencer(acgState.mixer.TickFrequency); acgState.synthesizers = new List <MySynthesizer>(); acgState.synthesizers.Add(new MySynthesizerPM8(acgState.mixer, numVoices)); acgState.synthesizers.Add(new MySynthesizerSS8(acgState.mixer, numVoices)); acgState.synthesizers.Add(new MySynthesizerCT8(acgState.mixer, numVoices)); acgState.mixer.TickCallback = () => acgState.sequencer.Tick(); jobListA = new LinkedList <System.Collections.IEnumerator>(); jobListB = new LinkedList <System.Collections.IEnumerator>(); coProviderA = providerA(); StartCoroutine(coProviderA); coProviderB = providerB(); StartCoroutine(coProviderB); #if UNITY_EDITOR LivingDead = true; #endif }
void OnDisable() { Synthesizer.Terminate(); Mixer.Terminate(); Sequencer = null; Synthesizer = null; Mixer = null; }
private LinkedList <float[]> generateAudioSamples() { MyMMLSequencer seq = station.acgState.sequencer; MyMixer mix = station.acgState.mixer; List <MySynthesizer> sss = station.acgState.synthesizers; if ((seq == null) || (mix == null) || (sss == null)) { return(null); } mix.Reset(); for (int j = 0; j < sss.Count; j++) { seq.SetSynthesizer(j, sss[j], toneSet, toneMap, 0xffffffffU); } seq.KeyShift = clip.Key; seq.VolumeScale = clip.Volume; seq.TempoScale = clip.Tempo; seq.Play(mml, 0.0f, false); LinkedList <float[]> temp = new LinkedList <float[]>(); const int workSize = 4096; float[] work = new float[workSize * AudioClipGeneratorState.numChannels]; bool zeroCross = false; for (;;) { if (station.disabled) { return(null); } if (seq.Playing) { mix.Update(); } Array.Clear(work, 0, work.Length); int numSamples = mix.Output(work, AudioClipGeneratorState.numChannels, workSize); if (numSamples == 0) { if (!zeroCross) { mix.Update(); continue; } break; } { float[] block = new float[numSamples * AudioClipGeneratorState.numChannels]; Array.Copy(work, block, numSamples * AudioClipGeneratorState.numChannels); temp.AddLast(block); float v0 = work[(numSamples - 1) * AudioClipGeneratorState.numChannels + 0]; float v1 = work[(numSamples - 1) * AudioClipGeneratorState.numChannels + 1]; zeroCross = (v0 == 0.0f) && (v1 == 0.0f); } } return(temp); }
// Use this for initialization void Start() { string defaultTone = "@pm8[7 0 op1[0 0 1 0 0 0 0 1 0 14 0 0 0 7] op2[0 0 1 0 0 0 0 1 127 31 0 0 0 15] op3[0 0 1 0 0 0 0 1 127 31 0 0 0 15] op4[0 0 1 0 0 0 0 1 127 31 0 0 0 15] lfo[0 127 0 0 31 0 0 0 0]]"; tone = new ToneParam(defaultTone); mix = new MyMixer(48000, false); // <-- raise worker thread! gameObject.AddComponent <MusicPlayer>(); //gameObject.GetComponent<MusicPlayer>().Synthesizer.Channel[0].Damper(127); gameObject.GetComponent <MusicPlayer>().Synthesizer.Channel[0].ProgramChange(tone); // <-- connect synthesizer to mixer }
void OnEnable() { Mixer = new MyMixer((UInt32)AudioSettings.outputSampleRate, false); Synthesizer = new MySynthesizerPM8(Mixer); Sequencer = new MyMMLSequencer(Mixer.TickFrequency); Mixer.TickCallback = () => { Sequencer.Tick(); }; //UnityEngine.Debug.Log("sampleRate=" + AudioSettings.outputSampleRate + " numVoices=" + Synthesizer.MaxNumVoices); int len; int num; AudioSettings.GetDSPBufferSize(out len, out num); //UnityEngine.Debug.Log("len=" + len); //UnityEngine.Debug.Log("num=" + num); }
void OnDestroy() { //UnityEngine.Debug.Log("OnDestroy"); StopCoroutine(coProviderA); StopCoroutine(coProviderB); jobListB = null; jobListA = null; foreach (var ss in synthesizers) { ss.Terminate(); } synthesizers = null; mixer.Terminate(); mixer = null; sequencers = null; foreach (var ss in acgState.synthesizers) { ss.Terminate(); } acgState.synthesizers = null; acgState.mixer.Terminate(); acgState.mixer = null; acgState.sequencer = null; }
public AudioClip CreateAudioClip(string name, MyMMLSequence mml, List <object> toneSet, Dictionary <int, int> toneMap) { const int frequency = 44100; const int numChannels = 2; MyMixer mix = new MyMixer(frequency, true); MySynthesizer ss0 = new MySynthesizerPM8(mix); MyMMLSequencer seq = new MyMMLSequencer(mix.TickFrequency); mix.TickCallback = () => seq.Tick(); seq.SetSynthesizer(0, ss0, toneSet, toneMap, 0xffffffffU); seq.Play(mml, 0.0f, false); int totalSamples = 0; LinkedList <float[]> temp = new LinkedList <float[]>(); const int workSize = 4096; float[] work = new float[workSize * numChannels]; bool first = true; for (;;) { Array.Clear(work, 0, work.Length); if (seq.Playing) { mix.Update(); } int numSamples = mix.Output(work, numChannels, workSize); if (numSamples == 0) { break; } if (first) { // skip leading zeros. for (var i = 0; i < numSamples; i++) { if ((work[i * 2 + 0] != 0.0f) || (work[i * 2 + 1] != 0.0f)) { first = false; numSamples -= i; float[] block = new float[numSamples * numChannels]; Array.Copy(work, i * 2, block, 0, numSamples * numChannels); temp.AddLast(block); break; } } } else { float[] block = new float[numSamples * numChannels]; Array.Copy(work, block, numSamples * numChannels); temp.AddLast(block); } totalSamples += numSamples; } AudioClip clip = AudioClip.Create(name, totalSamples, numChannels, frequency, false); int pos = 0; foreach (var block in temp) { clip.SetData(block, pos); pos += block.Length / numChannels; } ss0.Terminate(); mix.Terminate(); return(clip); }