Ejemplo n.º 1
0
 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;
     }
 }
Ejemplo n.º 2
0
        // Update is called once per frame
        //! [Example MPTK_PlayNotes]
        void Update()
        {
            // Checj 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;
            }

            //GetInput();
            GetArduinoInput();  // check for input
            PlayCheck();        // make sound for each str
        }
Ejemplo n.º 3
0
        // Update is called once per frame
        //! [Example MPTK_PlayNotes]
        void Update()
        {
            // Checj 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;
            }

            if (PitchChange != DEFAULT_PITCH)
            {
                // If user change the pitch, wait 1/2 second before return to median value
                if (Time.realtimeSinceStartup - LastTimePitchChange > 0.5f)
                {
                    PitchChange = Mathf.SmoothDamp(PitchChange, DEFAULT_PITCH, ref currentVelocityPitch, 0.5f);
                    if (Mathf.Abs(PitchChange - DEFAULT_PITCH) < 0.1f)
                    {
                        PitchChange = DEFAULT_PITCH;
                    }
                    //PitchChange = Mathf.Lerp(PitchChange, DEFAULT_PITCH, Time.deltaTime*10f);
                    //Debug.Log("DEFAULT_PITCH " + DEFAULT_PITCH + " " + PitchChange + " " + currentVelocityPitch);
                    midiStreamPlayer.MPTK_PlayEvent(new MPTKEvent()
                    {
                        Command = MPTKCommand.PitchWheelChange, Value = (int)PitchChange << 7, Channel = StreamChannel
                    });
                }
            }

            if (DrumKit)
            {
                // Set canal to dedicated drum canal (9 if canal start from 0, canal 10 is displayed in log)
                StreamChannel = 9;
            }
            else
            {
                StreamChannel = 0;
            }

            if (midiStreamPlayer != null && (IsplayingLoopPresets || IsplayingLoopNotes))
            {
                float time = Time.realtimeSinceStartup - LastTimeChange;
                if (time > DelayTimeChange)
                {
                    // It's time to generate some notes ;-)
                    LastTimeChange = Time.realtimeSinceStartup;

                    int noteToPlay;
                    if (ChordPlay)
                    {
                        noteToPlay = 3;
                    }
                    else if (ArpeggioPlay)
                    {
                        noteToPlay = 5;
                    }
                    else
                    {
                        noteToPlay = 1;
                    }

                    while (--noteToPlay >= 0)
                    {
                        if (IsplayingLoopPresets)
                        {
                            if (++CurrentPreset > EndPreset)
                            {
                                CurrentPreset = StartPreset;
                            }
                            if (CurrentPreset < StartPreset)
                            {
                                CurrentPreset = StartPreset;
                            }
                            midiStreamPlayer.MPTK_PlayEvent(new MPTKEvent()
                            {
                                Command = MPTKCommand.PatchChange, Value = CurrentPreset, Channel = StreamChannel,
                            });
                        }

                        if (IsplayingLoopNotes)
                        {
                            if (++CurrentNote > EndNote)
                            {
                                CurrentNote = StartNote;
                            }
                            if (CurrentNote < StartNote)
                            {
                                CurrentNote = StartNote;
                            }
                        }

                        // For chrod or arpeggiator
                        if (noteToPlay == 1)
                        {
                            CurrentNote += 3;
                        }
                        else if (noteToPlay == 2)
                        {
                            CurrentNote += 7;
                        }

                        if (RandomPlay)
                        {
                            CurrentNote += UnityEngine.Random.Range(-8, 8);
                        }

                        //Debug.LogFormat("Play {0}", CurrentNote);

                        long duration = Convert.ToInt64((DelayTimeChange + DeltaDelay) * 1000);
                        if (duration < 0)
                        {
                            duration = 1;
                        }

                        // Send the note to the player. Notes are plays in a thread, so call returns immediately
                        // Note is stopped automatically avec the Duration defined.
                        midiStreamPlayer.MPTK_PlayEvent(
                            new MPTKEvent()
                        {
                            Command  = MPTKCommand.NoteOn,
                            Value    = CurrentNote,
                            Channel  = StreamChannel,
                            Duration = duration,
                            Velocity = Velocity,
                        });
                    }
                }
            }
        }
Ejemplo n.º 4
0
        // Update is called once per frame
        void Update()
        {
            // Checj 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;
            }

            if (PitchChange != DEFAULT_PITCH)
            {
                // If user change the pitch, wait 1/2 second before return to median value
                if (Time.realtimeSinceStartup - LastTimePitchChange > 0.5f)
                {
                    PitchChange = Mathf.SmoothDamp(PitchChange, DEFAULT_PITCH, ref currentVelocityPitch, 0.5f);
                    if (Mathf.Abs(PitchChange - DEFAULT_PITCH) < 0.1f)
                    {
                        PitchChange = DEFAULT_PITCH;
                    }
                    //PitchChange = Mathf.Lerp(PitchChange, DEFAULT_PITCH, Time.deltaTime*10f);
                    //Debug.Log("DEFAULT_PITCH " + DEFAULT_PITCH + " " + PitchChange + " " + currentVelocityPitch);
                    midiStreamPlayer.MPTK_PlayEvent(new MPTKEvent()
                    {
                        Command = MPTKCommand.PitchWheelChange, Value = (int)PitchChange << 7, Channel = StreamChannel
                    });
                }
            }

            if (midiStreamPlayer != null && (IsplayingLoopPresets || IsplayingLoopNotes))
            {
                float time = Time.realtimeSinceStartup - LastTimeChange;
                if (time > Frequency)
                {
                    // It's time to generate some notes ;-)
                    LastTimeChange = Time.realtimeSinceStartup;


                    for (int indexNote = 0; indexNote < CountNoteToPlay; indexNote++)
                    {
                        if (IsplayingLoopPresets)
                        {
                            if (++CurrentPreset > EndPreset)
                            {
                                CurrentPreset = StartPreset;
                            }
                            if (CurrentPreset < StartPreset)
                            {
                                CurrentPreset = StartPreset;
                            }
                            midiStreamPlayer.MPTK_PlayEvent(new MPTKEvent()
                            {
                                Command = MPTKCommand.PatchChange,
                                Value   = CurrentPreset,
                                Channel = StreamChannel,
                            });
                        }

                        if (IsplayingLoopNotes)
                        {
                            if (++CurrentNote > EndNote)
                            {
                                CurrentNote = StartNote;
                            }
                            if (CurrentNote < StartNote)
                            {
                                CurrentNote = StartNote;
                            }
                        }

                        Play(false);
                    }
                }
            }
        }
Ejemplo n.º 5
0
        // Update is called once per frame
        //! [Example MPTK_PlayNotes]
        void Update()
        {
            // Checj 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;
            }

            if (midiStreamPlayer != null && IsplayingLoop)
            {
                float time = Time.realtimeSinceStartup - LastTimeChange;
                if (time > DelayTimeChange)
                {
                    // It's time to generate a note
                    LastTimeChange = Time.realtimeSinceStartup;

                    if (RandomPlay)
                    {
                        //
                        // First method to play notes: send a list of notes directly to the MidiStreamPlayer
                        // Useful for a long list of notes when the duration of the note is lnown.
                        //
                        List <MPTKEvent> notes = new List <MPTKEvent>();
                        // Very light random notes generator
                        if (!DrumKit)
                        {
                            // Play 3 notes with no delay
                            int rnd = UnityEngine.Random.Range(-8, 8);
                            notes.Add(CreateNote(60 + rnd, 0));
                            notes.Add(CreateNote(64 + rnd, 0));
                            notes.Add(CreateNote(67 + rnd, 0));
                        }
                        else
                        {
                            // Play 3 hit with a short delay
                            notes.Add(CreateDrum(UnityEngine.Random.Range(0, 127), 0));
                            notes.Add(CreateDrum(UnityEngine.Random.Range(0, 127), 150));
                            notes.Add(CreateDrum(UnityEngine.Random.Range(0, 127), 300));
                        }
                        // Send the note to the player. Notes are plays in a thread, so call returns immediately
                        midiStreamPlayer.MPTK_PlayEvent(notes);
                    }
                    else
                    {
                        //
                        // Second method to play and stop a notes: the duration is not known
                        // Here, a new note stop the previous
                        //
                        if (++CurrentNote > EndNote)
                        {
                            CurrentNote = StartNote;
                        }
                        if (CurrentNote < StartNote)
                        {
                            CurrentNote = StartNote;
                        }
                        PlayOneNote();
                    }
                }
            }
        }
        // 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;
            }
        }