Skip to content

Commit

Permalink
2.0.2.0 Improved MT-32 sounds output timing & latency.
Browse files Browse the repository at this point in the history
  • Loading branch information
110-kenichi committed Aug 5, 2020
1 parent 618c903 commit 83e11bd
Show file tree
Hide file tree
Showing 7 changed files with 44 additions and 33 deletions.
4 changes: 3 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
MAmidiMEmo V2.0.0.0 / Itoken (c)2019, 2020 / GPL-2.0
MAmidiMEmo V2.0.2.0 / Itoken (c)2019, 2020 / GPL-2.0

*** What is the MAmidiMEmo? ***

Expand Down Expand Up @@ -224,6 +224,8 @@ e.g.) YM2151 has 8ch FM sounds, so you can play 8 chords on MIDI 1ch or sharing

*** Changes ***

2.0.2.0 Improved MT-32 sounds output timing & latency.
2.0.1.0 Fixed crashing on some chip...
2.0.0.0 Fixed some minor bugs.
Panic button sometimes does not work.
SerializeData does not work and cause crash.
Expand Down
2 changes: 1 addition & 1 deletion src/devices/sound/cm32p.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -227,7 +227,7 @@ void cm32p_device::sound_stream_update(sound_stream &stream, stream_sample_t **i
stream_sample_t *buffer1 = outputs[0];
stream_sample_t *buffer2 = outputs[1];

if (!m_enable || memory_initialized == 0)
if (memory_initialized == 0)
{
memset(buffer1, 0, samples * sizeof(*buffer1));
memset(buffer2, 0, samples * sizeof(*buffer2));
Expand Down
45 changes: 21 additions & 24 deletions src/devices/sound/mt32.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ void mt32_device::device_start()
mt32emu_set_analog_output_mode(context, mt32emu_analog_output_mode::MT32EMU_AOM_ACCURATE);
mt32emu_set_samplerate_conversion_quality(context, mt32emu_samplerate_conversion_quality::MT32EMU_SRCQ_BEST);
mt32emu_select_renderer_type(context, mt32emu_renderer_type::MT32EMU_RT_FLOAT);
mt32emu_is_nice_partial_mixing_enabled(context);
mt32emu_set_nice_partial_mixing_enabled(context, mt32emu_boolean::MT32EMU_BOOL_TRUE);
mt32emu_preallocate_reverb_memory(context, mt32emu_boolean::MT32EMU_BOOL_TRUE);
mt32emu_set_midi_delay_mode(context, mt32emu_midi_delay_mode::MT32EMU_MDM_IMMEDIATE);
//mt32emu_set_output_gain(context, 0.75);
Expand All @@ -82,59 +82,56 @@ void mt32_device::device_start()
/** Enqueues a single short MIDI message to be processed ASAP. The message must contain a status byte. */
void mt32_device::play_msg(mt32emu_bit32u msg)
{
//*
mt32emu_play_msg(context, msg);
//*/
/*
mtxBuffer.lock();
mt32emu_play_msg_now(context, msg);

attotime diff = machine().time() - lastUpdateTime;
mt32emu_bit32u tim = mt32emu_get_internal_rendered_sample_count(context);
tim += (mt32emu_bit32u)round(diff.as_double() / mt32_tick.as_double());
mt32emu_play_msg_at(context, msg, tim);

mtxBuffer.unlock();
//*/
}
/** Enqueues a single well formed System Exclusive MIDI message to be processed ASAP. */
void mt32_device::play_sysex(const mt32emu_bit8u *sysex, mt32emu_bit32u len)
{
//*
mt32emu_play_sysex(context, sysex, len);
//*/
/*
mtxBuffer.lock();
mt32emu_play_sysex_now(context, sysex, len);

attotime diff = machine().time() - lastUpdateTime;
mt32emu_bit32u tim = mt32emu_get_internal_rendered_sample_count(context);
tim += (mt32emu_bit32u)round(diff.as_double() / mt32_tick.as_double());
mt32emu_play_sysex_at(context, sysex, len, tim);

mtxBuffer.unlock();
//*/
}

void mt32_device::sound_stream_update(sound_stream &stream, stream_sample_t **inputs, stream_sample_t **outputs, int samples)
{
stream_sample_t *buffer1 = outputs[0];
stream_sample_t *buffer2 = outputs[1];

if (!m_enable)
{
//memset(buffer1, 0, samples * sizeof(*buffer1));
//memset(buffer2, 0, samples * sizeof(*buffer2));
return;
}

float *orgptr = (float*)malloc(sizeof(float) * samples * 2);
float *renderBuf = orgptr;
float *streamBuf = orgptr;
//*
#if MAME_DEBUG
mtxBuffer.lock();
mt32emu_render_float(context, renderBuf, samples);
/*/
/*
lastUpdateTime = machine().time();
mtxBuffer.unlock();
#else
int inc = samples / 10;
int idx = 0;
for (idx = 0; idx < samples - inc; idx += inc, renderBuf += inc * 2)
{
mtxBuffer.lock();
mt32emu_render_float(context, renderBuf, inc);
lastUpdateTime = machine().time();
mtxBuffer.unlock();
}
mtxBuffer.lock();
mt32emu_render_float(context, renderBuf, samples - idx);
lastUpdateTime = machine().time();
mtxBuffer.unlock();
//*/
#endif

while (samples-- > 0)
{
Expand Down
2 changes: 2 additions & 0 deletions src/devices/sound/mt32.h
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,8 @@ class mt32_device : public device_t,
sound_stream *m_stream; /* stream number */
int m_frequency; /* set frequency - this can be changed using the appropriate function */
mt32emu_context context;
attotime lastUpdateTime;
attotime mt32_tick = attotime::from_hz(32000);

float clipping_overflow_l;
float clipping_overflow_r;
Expand Down
2 changes: 1 addition & 1 deletion src/mamidimemo/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ public static class Program
/// <summary>
///
/// </summary>
public const string FILE_VERSION = "2.0.1.0";
public const string FILE_VERSION = "2.0.2.0";

public const string FILE_COPYRIGHT = @"Virtual chiptune sound MIDI module ""MAmidiMEmo"" Version {0}
Copyright(C) 2019, 2020 Itoken.All rights reserved.";
Expand Down
3 changes: 3 additions & 0 deletions src/mamidimemo/instruments/Chips/CM32P.cs
Original file line number Diff line number Diff line change
Expand Up @@ -722,6 +722,9 @@ internal override void PrepareSound()

loadSfTable();
CM32PInitlaizeMemory(UnitNumber);

DeferredWriteData(SetOutputGain, UnitNumber, SoundInterfaceTagNamePrefix, 0, GainLeft);
DeferredWriteData(SetOutputGain, UnitNumber, SoundInterfaceTagNamePrefix, 1, GainRight);
}

private void loadSfTable()
Expand Down
19 changes: 13 additions & 6 deletions src/mamidimemo/instruments/Chips/MT32.cs
Original file line number Diff line number Diff line change
Expand Up @@ -369,7 +369,8 @@ private static delegate_mt32_play_sysex mt32_play_sysex
/// </summary>
private static void MT32PlayMsg(uint unitNumber, uint msg)
{
DeferredWriteData(mt32_play_msg, unitNumber, msg);
mt32_play_msg(unitNumber, msg);
//DeferredWriteData(mt32_play_msg, unitNumber, msg);
/*
try
{
Expand All @@ -388,7 +389,8 @@ private static void MT32PlayMsg(uint unitNumber, uint msg)
/// </summary>
private static void MT32PlaySysEx(uint unitNumber, byte[] sysex)
{
DeferredWriteData(mt32_play_sysex, unitNumber, sysex, sysex.Length);
mt32_play_sysex(unitNumber, sysex, sysex.Length);
//DeferredWriteData(mt32_play_sysex, unitNumber, sysex, sysex.Length);
/*
try
{
Expand Down Expand Up @@ -448,6 +450,14 @@ public MT32(uint unitNumber) : base(unitNumber)
Timbres[i] = new MT32Timbre();
}

internal override void PrepareSound()
{
base.PrepareSound();

DeferredWriteData(SetOutputGain, UnitNumber, SoundInterfaceTagNamePrefix, 0, GainLeft);
DeferredWriteData(SetOutputGain, UnitNumber, SoundInterfaceTagNamePrefix, 1, GainRight);
}

protected override void OnMidiEvent(MidiEvent midiEvent)
{
uint msg = 0;
Expand All @@ -468,10 +478,7 @@ protected override void OnMidiEvent(MidiEvent midiEvent)
}
case NoteOnEvent non:
{
if (non.Velocity == 0)
msg = (uint)((0x80 | non.Channel) | non.NoteNumber << 8 | non.Velocity << 16);
else
msg = (uint)((0x90 | non.Channel) | non.NoteNumber << 8 | non.Velocity << 16);
msg = (uint)((0x90 | non.Channel) | non.NoteNumber << 8 | non.Velocity << 16);
break;
}
case NoteAftertouchEvent na:
Expand Down

0 comments on commit 83e11bd

Please sign in to comment.