homy-game-studio / hgs-unity-tone

Simple package to play musical notes
MIT License
23 stars 1 forks source link
audio keyboard midi music note piano rhythm synthesizer unity

semantic-release openupm

Introduction

HGS Tone is a Synthsizer and Midi Player wrapper of MeltySynth for Unity. This package turns possible play musical notes from all part of your UnityProject. Click in this image above to see youtube vídeo.

Requires Unity 2021 or bigger.

Open Source Midi Player - Unity HGS ToneERE

Playing Musical Notes

Play note and release after default time

using HGS.Tone

public class Player: MonoBehaviour
{
  [SerializeField] ToneSynth synth;

  void Start()
  {
   synth.TriggerAttackAndRelease(ToneNote.Parse("C3"));
  }
}

Play note and release after 3 seconds

using HGS.Tone

public class Player: MonoBehaviour
{
  [SerializeField] ToneSynth synth;

  void Start()
  {
   synth.TriggerAttackAndRelease(ToneNote.Parse("C3"), duration:3);
  }
}

Play note with custom release

using HGS.Tone

public class Player: MonoBehaviour
{
  [SerializeField] ToneSynth synth;

  void Update()
  {
    if(Input.GetKeyDown(KeyCode.C)) synth.TriggerAttack(ToneNote.Parse("C3"));
    if(Input.GetKeyUp(KeyCode.C)) synth.TriggerRelease(ToneNote.Parse("C3"));
  }
}

Changing instrument

Use MidiInstrumentCode to access Midi instrument list available.

using HGS.Tone

public class Player: MonoBehaviour
{
  [SerializeField] ToneSynth synth;

  void Start()
  {
    synth.SetInstrument(MidiInstrumentCode.Organ_Accordion);
  }
}

Playing Midi files

From Resources folder | Attention: for read midi files from resources, you need change file extension from .midi to .bytes.

using HGS.Tone

public class Player: MonoBehaviour
{
  [SerializeField] ToneSequencer sequencer;

  void Start()
  {
    var path = "you/path/to/midifile";
    sequencer.Play(path);
  }
}

From others sources

using HGS.Tone

public class Player: MonoBehaviour
{
  [SerializeField] ToneSequencer sequencer;

  void Start()
  {
    var bytes = new byte[]{}; // you file bytes
    var stream = new MemoryStream(bytes);
    sequencer.Play(stream);
  }
}

MidiMessage Events

ToneSequencer and ToneSynth contains onMidiMessage:

using HGS.Tone

public class CallbackLogger: MonoBehaviour
{
  [SerializeField] ToneSequencer sequencer;

  void Start()
  {
    sequencer.onMidiMessage += HandleOnMidiMessage;
  }

  void HandleOnMidiMessage(int channel, int command, int data1, int data2)
  {
    Debug.Log($"MidiMessageCallback: {channel}, {command}, {data1}, {data2}");
  }
}

Installation

OpenUPM:

openupm add com.hgs.tone

Package Manager:

https://github.com/homy-game-studio/hgs-unity-tone.git#upm

Or specify version:

https://github.com/homy-game-studio/hgs-unity-tone.git#1.0.0

Samples

You can see all samples directly in Package Manager window.

Contrib

If you found any bugs, have any suggestions or questions, please create an issue on github. If you want to contribute code, fork the project and follow the best practices below, and make a pull request.

Namespace Convention

To avoid script collisions, all scripts of this package is covered by HGS.Tone namespace.

Branchs

Whenever a change is detected on the master branch, CI gets the contents of Assets/Package, and pushes in upm branch.

Commit Convention

This package uses semantic-release to facilitate the release and versioning system. Please use angular commit convention:

<type>(<scope>): <short summary>
  │       │             │
  │       │             └─⫸ Summary in present tense. Not capitalized. No period at the end.
  │       │
  │       └─⫸ Commit Scope: Namespace, script name, etc..
  │
  └─⫸ Commit Type: build|ci|docs|feat|fix|perf|refactor|test

Type.: