It is possible to play note directly without read midi file? #231
-
If I want to create piano in Unity how can I play a note without create and read midi file or just create once and play it? |
Beta Was this translation helpful? Give feedback.
Replies: 3 comments 13 replies
-
You can always create var tempoMap = TempoMap.Default;
var playback = new Playback(
new[]
{
new Note(NoteName.ASharp, 2).SetLength(MusicalTimeSpan.Quarter, tempoMap)
},
tempoMap); Here we create playback for single quarter note – A#2. Of course you can pass output device to the Also if you create piano, maybe it's better to just send a Note On on key press and a Note Off on key release? |
Beta Was this translation helpful? Give feedback.
-
using System;
using System.Linq;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using Melanchall.DryWetMidi.Core;
using Melanchall.DryWetMidi.Interaction;
using Melanchall.DryWetMidi.Multimedia;
using Melanchall.DryWetMidi.MusicTheory;
public class PlayNote : MonoBehaviour
{
private const string OutputDeviceName = "Microsoft GS Wavetable Synth";
private OutputDevice _outputDevice;
private Playback _playback;
void Start()
{
InitializeOutputDevice();
}
private void InitializeOutputDevice()
{
Debug.Log($"Initializing output device [{OutputDeviceName}]...");
var allOutputDevices = OutputDevice.GetAll();
if (!allOutputDevices.Any(d => d.Name == OutputDeviceName))
{
var allDevicesList = string.Join(Environment.NewLine, allOutputDevices.Select(d => $" {d.Name}"));
Debug.Log($"There is no [{OutputDeviceName}] device presented in the system. Here the list of all device:{Environment.NewLine}{allDevicesList}");
return;
}
_outputDevice = OutputDevice.GetByName(OutputDeviceName);
Debug.Log($"Output device [{OutputDeviceName}] initialized.");
}
void Update()
{
if (Input.GetKeyDown(KeyCode.Space))
{
PlayOneNote();
}
}
private void PlayOneNote()
{
Debug.Log("Attempting to play...");
var tempoMap = TempoMap.Default;
_playback = new Playback(
new[]
{
new Melanchall.DryWetMidi.Interaction.Note(NoteName.ASharp, 2).SetLength(MusicalTimeSpan.Quarter, tempoMap)
},
tempoMap, _outputDevice);
_playback.Start();
}
} As you can see, the only change I made was to make _playback be stored privately, out of scope from the method so it is not collected by the GC. In Unity, there is a delay of about 0.5s. After hitting space and hearing the A#. |
Beta Was this translation helpful? Give feedback.
-
Update occurs every frame so that's not the issue. Thank you very much for
all your help regardless. Time for me to take a different approach!
Please add this information to the Unity package site to let others know
realtime sounds are not feasible if you need low latency.
…On Tue, 21 Mar 2023, 13:26 Maxim Dobroselsky, ***@***.***> wrote:
Hi,
Unfortunately producing the audio is not related with my library. Looking
at the second dump I see that all MIDI events are sent to output device at
the correct times. So I can't say why Windows (or sound card or something
else) adds delay. I can help with DryWetMIDI only, but I don't see any
issues.
There is a chance this
void Update()
{
if (Input.GetKeyDown(KeyCode.Space))
is the place where delay occurs. I don't know how fast Update is called.
You can place logging there:
void Update()
{
Debug.Log("AAA");
if (Input.GetKeyDown(KeyCode.Space))
Then press space and look at the console how fast AAA appears.
—
Reply to this email directly, view it on GitHub
<#231 (reply in thread)>,
or unsubscribe
<https://github.com/notifications/unsubscribe-auth/ANLFFOMQJVMJKMLXUO2I2PDW5GT7TANCNFSM6AAAAAASOCCCUU>
.
You are receiving this because you were mentioned.Message ID:
***@***.***>
|
Beta Was this translation helpful? Give feedback.
You can always create
Playback
manually from desired objects:Here we create playback for single quarter note – A#2. Of course you can pass output device to the
Playback
's constructor. Please read documentation on the playback, you'll find there all required info – https://melanchall.github.io/drywetmidi/api/Melanchall.DryWetMidi.Multimedia.Playback.html.Also if you create piano, maybe it's better to just send a Note On on key press and a Note Off on key release?