Exemplo n.º 1
0
 public void EndLoadingSynth(string name)
 {
     Debug.LogFormat("Synth {0} loaded", name);
     midiStreamPlayer.MPTK_PlayEvent(new MPTKEvent()
     {
         Command = MPTKCommand.PatchChange, Value = CurrentPatchInstrument, Channel = StreamChannel,
     });
 }
Exemplo n.º 2
0
        // Use this for initialization
        void Start()
        {
            if (midiStreamPlayer != null)
            {
                if (!midiStreamPlayer.OnEventSynthStarted.HasEvent())
                {
                    midiStreamPlayer.OnEventSynthStarted.AddListener(StartLoadingSynth);
                }
                if (!midiStreamPlayer.OnEventSynthStarted.HasEvent())
                {
                    midiStreamPlayer.OnEventSynthStarted.AddListener(EndLoadingSynth);
                }
            }
            else
            {
                Debug.LogWarning("No Stream Midi Player associed to this game object");
            }

            CurrentNote = StartNote;

            midiStreamPlayer.MPTK_PlayEvent(new MPTKEvent()
            {
                Command = MPTKCommand.PatchChange, Value = CurrentPreset, Channel = StreamChannel,
            });
        }
        // Update is called once per frame
        void Update()
        {
            // Check that SoundFont is loaded and add a little wait (0.5 s by default) because Unity AudioSource need some time to be started
            if (!MidiPlayerGlobal.MPTK_IsReady())
            {
                return;
            }

            //create notes here!!!
            if (midiStreamPlayer != null)
            {
                //update midi variables if necessary
                midiStreamPlayer.MPTK_PlayEvent(new MPTKEvent()
                {
                    Command = MPTKCommand.PatchChange,
                    Value   = instrumentSound,
                    Channel = streamChannel,
                });


                //velocity handeling
                if (velocity <= 40 && velocity > 0)
                {
                    velocity = 40;
                }

                if (velocity >= 127)
                {
                    velocity = 127;
                }

                if (velocity == 0)
                {
                    StopOneNote(); //note stops when velocity stops
                    newNote = true;
                }

                //pitch handeling
                if (sensorPitch == 500)
                {
                    pitchChange = DEFAULT_PITCH;
                }
                else
                {
                    pitchChange = sensorPitch / 7.8f; //translate 0-1000 roughly to 0-127
                }
            }

            //only play if all components are active
            if (velocity != 0 && currentNote != -1 && pitchChange != 64)
            {
                isPlaying = true;
                if (newNote)
                {
                    Play(false);
                    //visualizes in main view
                    mainInstrumentVisualizer.GetComponent <MainVisualizer>().NewNote(velocity, currentNote, pitchChange);
                    mainInstrumentVisualizer.GetComponent <MainVisualizer>().isStopped = false;
                    newNote = false;
                }
            }
            else
            {
                isPlaying = false;
                mainInstrumentVisualizer.GetComponent <MainVisualizer>().isStopped = true;
            }
        }