Skip to content

Instantly share code, notes, and snippets.

@kshoji
Created May 22, 2024 09:59
Show Gist options
  • Save kshoji/ebd5b3b6d4e3e3fe5746e494aafa289a to your computer and use it in GitHub Desktop.
Save kshoji/ebd5b3b6d4e3e3fe5746e494aafa289a to your computer and use it in GitHub Desktop.
The sequence recording part of MidiSampleScene.cs
if (GUILayout.Button("Start recording SMF") && isSequencerOpened)
{
var transmitters = new HashSet<ITransmitter>();
if (selectedInputDeviceId != null)
{
var transmitter = MidiSystem.GetTransmitter(selectedOutputDeviceId);
if (transmitter != null)
{
transmitters.Add(transmitter);
}
}
sequencer.UpdateDeviceConnections(new HashSet<IReceiver>(), transmitters);
sequencer.SetSequence(new Sequence(Sequence.Ppq, 480));
if (sequencer.GetIsRecording())
{
sequencer.StopRecording();
}
sequencer.StartRecording();
}
var recordedSmfPath = Path.Combine(Application.persistentDataPath, "record.mid");
if (GUILayout.Button("Stop recording") && isSequencerOpened)
{
sequencer.Stop();
var sequence = sequencer.GetSequence();
if (sequence.GetTickLength() > 0)
{
using (var stream = new FileStream(recordedSmfPath, FileMode.Create, FileAccess.Write))
{
MidiSystem.WriteSequence(sequence, stream);
}
}
}
if (File.Exists(recordedSmfPath) && GUILayout.Button("Play recorded SMF") && isSequencerOpened)
{
using (var stream = new FileStream(recordedSmfPath, FileMode.Open, FileAccess.Read))
{
UpdateReceiverConnections();
sequencer.Stop();
sequencer.SetSequence(stream);
sequencer.Start();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment