public bool LoadMidi(MidiFile midi, bool UnloadUnusedInstruments)
 {
     if (playing == true)
     {
         return(false);
     }
     _MidiFile = midi;
     if (_MidiFile.SequencerReady == false)
     {
         try
         {
             //Combine all tracks into 1 track that is organized from lowest to highest abs time
             _MidiFile.CombineTracks();
             //Convert delta time to sample time
             eventIndex = 0;
             uint lastSample = 0;
             for (int x = 0; x < _MidiFile.Tracks[0].MidiEvents.Length; x++)
             {
                 _MidiFile.Tracks[0].MidiEvents[x].deltaTime = lastSample + (uint)DeltaTimetoSamples(_MidiFile.Tracks[0].MidiEvents[x].deltaTime);
                 lastSample = _MidiFile.Tracks[0].MidiEvents[x].deltaTime;
                 //Update tempo
                 if (_MidiFile.Tracks[0].MidiEvents[x].midiMetaEvent == MidiHelper.MidiMetaEvent.Tempo)
                 {
                     _MidiFile.BeatsPerMinute = MidiHelper.MicroSecondsPerMinute / System.Convert.ToUInt32(_MidiFile.Tracks[0].MidiEvents[x].Parameters[0]);
                 }
             }
             //Set total time to proper value
             _MidiFile.Tracks[0].TotalTime = _MidiFile.Tracks[0].MidiEvents[_MidiFile.Tracks[0].MidiEvents.Length - 1].deltaTime;
             //reset tempo
             _MidiFile.BeatsPerMinute = 120;
             //mark midi as ready for sequencing
             _MidiFile.SequencerReady = true;
         }
         catch (Exception ex)
         {
             //UnitySynth
             Debug.Log("Error Loading Midi:\n" + ex.Message);
             return(false);
         }
     }
     blockList.Clear();
     if (UnloadUnusedInstruments == true)
     {
         if (synth.SoundBank == null)
         {//If there is no bank warn the developer =)
             Debug.Log("No Soundbank loaded !");
         }
         else
         {
             string bankStr = synth.SoundBank.BankPath;
             //Remove old bank being used by synth
             synth.UnloadBank();
             //Add the bank and switch to it with the synth
             BankManager.addBank(new InstrumentBank(synth.SampleRate, bankStr, _MidiFile.Tracks[0].Programs, _MidiFile.Tracks[0].DrumPrograms));
             synth.SwitchBank(BankManager.Count - 1);
         }
     }
     return(true);
 }
Exemple #2
0
        protected override void OpenMediaAsync()
        {
            System.Diagnostics.Debug.WriteLine("OpenMediaAsync");
            Dictionary <MediaStreamAttributeKeys, string>  streamAttributes = new Dictionary <MediaStreamAttributeKeys, string>();
            Dictionary <MediaSourceAttributesKeys, string> sourceAttributes = new Dictionary <MediaSourceAttributesKeys, string>();
            List <MediaStreamDescription> availableStreams = new List <MediaStreamDescription>();
            string codec = "";

            codec += ToLittleEndianString(string.Format("{0:X4}", 1));
            codec += ToLittleEndianString(string.Format("{0:X4}", 1));
            codec += ToLittleEndianString(string.Format("{0:X8}", SamplesPerSec));
            codec += ToLittleEndianString(string.Format("{0:X8}", AvgBytesPerSec));
            codec += ToLittleEndianString(string.Format("{0:X4}", BlockAlign));
            codec += ToLittleEndianString(string.Format("{0:X4}", BitsPerSample));
            codec += ToLittleEndianString(string.Format("{0:X4}", 0));

            streamAttributes[MediaStreamAttributeKeys.CodecPrivateData] = codec;
            sourceAttributes[MediaSourceAttributesKeys.Duration]        = "0";
            sourceAttributes[MediaSourceAttributesKeys.CanSeek]         = "false";

            MediaStreamDescription msd = new MediaStreamDescription(MediaStreamType.Audio, streamAttributes);

            _audioDesc = msd;
            availableStreams.Add(_audioDesc);


            String bankName = "SoundBanks/GMBank/gmpiano.txt";
            var    file     = Application.GetResourceStream(new Uri(bankName, UriKind.Relative));
            String result   = "";

            using (var reader = new StreamReader(file.Stream))
            {
                result += reader.ReadToEnd();
            }
            synth = new StreamSynthesizer(44100, 1, 1000, 2);

            CSharpSynth.Banks.BankManager.addBank(new InstrumentBank(44100, bankName));
            int bankfile = BankManager.Count - 1;

            synth.SwitchBank(bankfile);


            byte[] buffer = new byte[synth.BufferSize];

            midiSeq = new MidiSequencer(synth);
            midiSeq.LoadMidi("temp.mid", false);
            //System.Diagnostics.Debug.WriteLine("midi file is ");
            //System.Diagnostics.Debug.WriteLine(midifile.OriginalString);
            //midiSeq.LoadMidi(midifile.OriginalString, false);
            midiSeq.Looping = true;
            midiSeq.Play();
            startTime = DateTime.Now;
            ReportOpenMediaCompleted(sourceAttributes, availableStreams);
        }