Beispiel #1
0
    public void HitBall(int ballnum, int strength)
    {
        if (!setup)
        {
            return;
        }
        float noteal = minKey + winding + ballstep * (ballnum - 1);
        int   note   = Mathf.FloorToInt(noteal);

#if UNITY_EDITOR
        //  Debug.LogFormat("Hitting {0} on {1}", note,bank.GetPatch(0, programNum).Name);
#endif
        //  note = 75;

        //Debug.LogFormat("Hitting note: {0} {1} {2},   {3} {4} pn: {5} {6} {7}",note,ballnum,minKey,ballstep,winding, programNum,strength,setup);
        synthesizer.NoteOffAll(false);

        try
        {
            synthesizer.NoteOn(1, note, strength);
        }
        catch
        {
        }
    }
Beispiel #2
0
    void Update()

    {
        dt += Time.deltaTime;
        if (dt > 1)
        {
            //synthesizer.ProcessMidiMessage()
            if (Random.Range(0.0f, 1.0f) > 0.5f)
            {
                synthesizer.NoteOn(1, 60, 100);
            }
            else
            {
                synthesizer.NoteOn(1, 50, 100);
            }


            changeProgram();
            dt = 0;
        }
    }
        public void Awake()
        {
            synthesizer = new Synthesizer(sampleRate, channel, bufferSize, 1);
            sequencer   = new MidiFileSequencer(synthesizer);
            audioSource = GetComponent <AudioSource>();

            if (loadOnAwake)
            {
                LoadBank(new PatchBank(bankSource));
                LoadMidi(new MidiFile(midiSource));
            }

            if (playOnAwake)
            {
                //Play();
                audioSource.Play();
                Thread.Sleep(2000);
                synthesizer.NoteOn(0, 60, 120);
            }
        }
Beispiel #4
0
            private void SendEvents(Synthesizer synthesizer)
            {
                for (var i = 0; i < eventCount; i++)
                {
                    var me = events[i];
                    switch (me.Type)
                    {
                    case 0:     // RELEASE NOTE
                        synthesizer.NoteOff(me.Channel, me.Data1);
                        break;

                    case 1:     // PLAY NOTE
                        synthesizer.NoteOn(me.Channel, me.Data1, me.Data2);
                        break;

                    case 2:     // PITCH WHEEL
                        synthesizer.ProcessMidiMessage(me.Channel, 0xE0, me.Data1, me.Data2);
                        break;

                    case 3:     // SYSTEM EVENT
                        switch (me.Data1)
                        {
                        case 11:         // ALL NOTES OFF
                            synthesizer.NoteOffAll(true);
                            break;

                        case 14:         // RESET ALL CONTROLS
                            synthesizer.ResetSynthControls();
                            break;
                        }
                        break;

                    case 4:     // CONTROL CHANGE
                        switch (me.Data1)
                        {
                        case 0:         // PROGRAM CHANGE
                            if (me.Channel == 9)
                            {
                                break;
                            }
                            synthesizer.ProcessMidiMessage(me.Channel, 0xC0, me.Data2, 0);
                            break;

                        case 1:         // BANK SELECTION
                            synthesizer.ProcessMidiMessage(me.Channel, 0xB0, 0x00, me.Data2);
                            break;

                        case 2:         // MODULATION
                            synthesizer.ProcessMidiMessage(me.Channel, 0xB0, 0x01, me.Data2);
                            break;

                        case 3:         // VOLUME
                            synthesizer.ProcessMidiMessage(me.Channel, 0xB0, 0x07, me.Data2);
                            break;

                        case 4:         // PAN
                            synthesizer.ProcessMidiMessage(me.Channel, 0xB0, 0x0A, me.Data2);
                            break;

                        case 5:         // EXPRESSION
                            synthesizer.ProcessMidiMessage(me.Channel, 0xB0, 0x0B, me.Data2);
                            break;

                        case 8:         // PEDAL
                            synthesizer.ProcessMidiMessage(me.Channel, 0xB0, 0x40, me.Data2);
                            break;
                        }
                        break;
                    }
                }
            }
Beispiel #5
0
 public void NoteOn(int channel, int note, int velocity)
 {
     synth.NoteOn(channel, (short)note, (short)velocity);
 }
 public void StartNote(int note, int volume)
 {
     synthesizer.NoteOn(channel, note, volume);
 }
Beispiel #7
0
    // Now the synthesizer seems to work, but everything is played as piano. Why???
    public static void Test(byte[] data)
    {
        var sf     = 44100;
        var format = new WaveFormat(sf, 16, 2);
        var writer = new WaveFileWriter("out.wav", format);

        CheckHeader(data);

        var synth = new Synthesizer(sf, 2, sf / 140, 1);

        synth.LoadBank("GMBank.bank");
        var outBuf = new byte[synth.RawBufferSize];

        var scoreLen     = (int)BitConverter.ToUInt16(data, 4);
        var scoreStart   = (int)BitConverter.ToUInt16(data, 6);
        var channels     = (int)BitConverter.ToUInt16(data, 8);
        var sec_channels = (int)BitConverter.ToUInt16(data, 10);
        var instrCnt     = (int)BitConverter.ToUInt16(data, 12);
        var instruments  = new int[instrCnt];

        for (var i = 0; i < instruments.Length; i++)
        {
            instruments[i] = (int)BitConverter.ToUInt16(data, 16 + 2 * i);
        }

        Console.WriteLine("scoreLen = " + scoreLen);
        Console.WriteLine("scoreStart = " + scoreStart);
        Console.WriteLine("channels = " + channels);
        Console.WriteLine("sec_channels = " + sec_channels);
        Console.WriteLine("instrCnt = " + instrCnt);
        for (var i = 0; i < instruments.Length; i++)
        {
            Console.WriteLine("instruments[" + i + "] = " + instruments[i]);
        }

        Console.WriteLine();
        Console.WriteLine();
        Console.WriteLine();

        var totalTime = 0;

        var synthSamples = 0;

        var p        = scoreStart;
        var actions  = new List <Action>();
        var lastVels = new int[16];

        while (true)
        {
            var channelNumber = data[p] & 0xF;
            var eventType     = (data[p] & 0x70) >> 4;
            var last          = (data[p] >> 7) != 0;

            Console.WriteLine("channelNumber = " + channelNumber);
            Console.WriteLine("eventType = " + eventType);
            Console.WriteLine("last = " + last);

            p++;

            switch (eventType)
            {
            case 0:
                var releaseNote = data[p++];

                Console.WriteLine("    // Release note");
                Console.WriteLine("    releaseNote = " + releaseNote);

                actions.Add(() =>
                {
                    var cn = channelNumber;
                    var rn = releaseNote;
                    if (cn == 15)
                    {
                        cn = 9;
                    }
                    synth.NoteOff(cn, rn);
                });

                break;

            case 1:
                var playNote   = data[p++];
                var noteNumber = playNote & 127;
                var noteVolume = (playNote & 128) != 0 ? data[p++] : -1;

                Console.WriteLine("    // Play note");
                Console.WriteLine("    playNote = " + playNote);
                if (noteVolume != -1)
                {
                    Console.WriteLine("    noteVolume = " + noteVolume);
                }

                actions.Add(() =>
                {
                    var cn = channelNumber;
                    var nn = noteNumber;
                    var nv = noteVolume;
                    if (cn == 15)
                    {
                        cn = 9;
                    }
                    if (nv != -1)
                    {
                        lastVels[cn] = nv;
                    }
                    else
                    {
                        nv = lastVels[cn];
                    }
                    synth.NoteOn(cn, nn, nv);
                });

                break;

            case 2:
                var pitchWheel = data[p++];

                Console.WriteLine("    // Pitch wheel");
                Console.WriteLine("    pitchWheel = " + pitchWheel);

                actions.Add(() =>
                {
                    var cn = channelNumber;
                    var pw = pitchWheel;
                    if (cn == 15)
                    {
                        cn = 9;
                    }
                    var pw2 = (pw << 7) / 2;
                    var pw1 = pw2 & 127;
                    pw2   >>= 7;
                    synth.ProcessMidiMessage(cn, 0xE0, pw1, pw2);
                });

                break;

            case 3:
                var systemEvent = data[p++];

                Console.WriteLine("    // System event");
                Console.WriteLine("    systemEvent = " + systemEvent);

                //Console.ReadKey();

                break;

            case 4:
                var controllerNumber = data[p++];
                var controllerValue  = data[p++];

                Console.WriteLine("    // Change controller");
                Console.WriteLine("    controllerNumber = " + controllerNumber);
                Console.WriteLine("    controllerValue = " + controllerValue);

                if (controllerNumber == 0)
                {
                    actions.Add(() =>
                    {
                        var cn = channelNumber;
                        var cv = controllerValue;
                        synth.ProcessMidiMessage(cn, 0xC0, cv, 0);
                    });
                }
                else if (controllerNumber == 3)
                {
                    actions.Add(() =>
                    {
                        var cn = channelNumber;
                        var cv = controllerValue;
                        synth.ProcessMidiMessage(cn, 0xB0, 0x07, cv);
                    });
                }
                else if (controllerNumber == 4)
                {
                    actions.Add(() =>
                    {
                        var cn = channelNumber;
                        var cv = controllerValue;
                        synth.ProcessMidiMessage(cn, 0xB0, 0x0A, cv);
                    });
                }
                else if (controllerNumber == 5)
                {
                    actions.Add(() =>
                    {
                        var cn = channelNumber;
                        var cv = controllerValue;
                        synth.ProcessMidiMessage(cn, 0xB0, 0x0B, cv);
                    });
                }
                else
                {
                    //Console.ReadKey();
                }

                break;

            //case 5:
            //break;

            case 6:
                Console.WriteLine("END!!!");
                goto end;

            //case 7:
            //break;

            default:
                throw new Exception("OOPS!!!");
            }

            Console.WriteLine();

            if (last)
            {
                var time = 0;
                while (true)
                {
                    var value = data[p++];
                    time = time * 128 + (value & 127);
                    if ((value & 128) == 0)
                    {
                        break;
                    }
                }
                Console.WriteLine("================================================================================");
                Console.WriteLine(" TIME: " + time);
                Console.WriteLine("================================================================================");
                Console.WriteLine();

                totalTime += time;

restart:
                var totalSec = totalTime / 140.0;
                var synthSec = (double)synthSamples / synth.SampleRate;
                if (synthSec < totalSec)
                {
                    foreach (var action in actions)
                    {
                        action();
                    }
                    actions.Clear();

                    synth.GetNext(outBuf);
                    writer.Write(outBuf, 0, outBuf.Length);
                    synthSamples += outBuf.Length / 4;

                    goto restart;
                }
            }
        }

end:
        Console.WriteLine();
        Console.WriteLine();
        Console.WriteLine("TOTAL TIME: " + totalTime + " (" + (totalTime / 140.0) + " sec)");
        Console.WriteLine(p + " / " + data.Length);

        writer.Dispose();
    }
Beispiel #8
0
 public void NoteOn(int channel, int note, int velocity)
 {
     midiStreamSynthesizer.NoteOn(channel, note, velocity);
 }