Giter Club home page Giter Club logo

hgs-unity-tone's Introduction

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

  • master -> Keeps the unity project to development purposes.
  • upm -> Copy of folder content Assets/Package to release after pull request in master.

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.:

  • build: Changes that affect the build system or external dependencies (example scopes: package system)
  • ci: Changes to our CI configuration files and scripts (example scopes: Circle, - BrowserStack, SauceLabs)
  • docs: Documentation only changes
  • feat: A new feature
  • fix: A bug fix
  • perf: A code change that improves performance
  • refactor: A code change that neither fixes a bug nor adds a feature
  • test: Adding missing tests or correcting existing tests

hgs-unity-tone's People

Contributors

matheusmoura17 avatar semantic-release-bot avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar

Forkers

unitycoder

hgs-unity-tone's Issues

Not working in IL2CPP

Symptoms

Extremely slow audio. There are also too many memory allocations, possible bug during MeltySynth conversion to cpp.

Change soundfont

Hi, thank you for port this to unity
I want to ask about the SoundFont
MeltySynth mentioned this code

// Create the synthesizer. var sampleRate = 44100; var synthesizer = new Synthesizer("TimGM6mb.sf2", sampleRate);

I haven't tried MeltySynth or your library
but is it possible to change SoundFont runtime, and choose the instrument just like you did on the keyboard demo?
I would like to clone Punk o Matic game but using midi

Add instrument change event.

Expected

ToneSequencer.onInstrumentChange.AddListener(HandleOnInstrumentChange)

void HandleOnInstrumentChange(int  chanel,  int  instrumentNumber){
 // logic here
}
ToneSynth.onInstrumentChange.AddListener(HandleOnInstrumentChange)

void HandleOnInstrumentChange(int  chanel,  int  instrumentNumber){
 // logic here
}

Add note attack/release events

expected:

ToneSequencer.OnNoteAttack.AddListener(HandleOnNoteAttack);
ToneSequencer.OnNoteRelease.AddListener(HandleOnNoteRelease);

void HandleOnNoteAttack(int  chanel,  ToneNote note){
 // logic here
}

void HandleOnNoteRelease(int  chanel,  ToneNote note){
 // logic here
}
ToneSynth.OnNoteAttack.AddListener(HandleOnNoteAttack);
ToneSynth.OnNoteRelease.AddListener(HandleOnNoteRelease);

void HandleOnNoteAttack(int  chanel,  ToneNote note){
 // logic here
}

void HandleOnNoteRelease(int  chanel,  ToneNote note){
 // logic here
}

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.