aboutsummaryrefslogtreecommitdiff
path: root/utils/winmm_drv/src/MidiSynth.cpp
blob: 078d930ac9393a8b37278a791c8ae9746a091fda (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
/* Copyright (C) 2011, 2012 Sergey V. Mikayev
 *
 *  This program is free software: you can redistribute it and/or modify
 *  it under the terms of the GNU Lesser General Public License as published by
 *  the Free Software Foundation, either version 2.1 of the License, or
 *  (at your option) any later version.
 *
 *  This program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU Lesser General Public License for more details.
 *
 *  You should have received a copy of the GNU Lesser General Public License
 *  along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

#include "stdafx.h"

namespace OPL3Emu
{

#define DRIVER_MODE

static MidiSynth &midiSynth = MidiSynth::getInstance();

static const unsigned int s_audioChannels = 2;

static class MidiStream
{
private:
    static const unsigned int maxPos = 1024;
    unsigned int startpos;
    unsigned int endpos;
    DWORD stream[maxPos][2];

public:
    MidiStream()
    {
        startpos = 0;
        endpos = 0;
    }

    DWORD PutMessage(DWORD msg, DWORD timestamp)
    {
        unsigned int newEndpos = endpos;

        newEndpos++;
        if(newEndpos == maxPos)  // check for buffer rolloff
            newEndpos = 0;
        if(startpos == newEndpos)  // check for buffer full
            return -1;
        stream[endpos][0] = msg;    // ok to put data and update endpos
        stream[endpos][1] = timestamp;
        endpos = newEndpos;
        return 0;
    }

    DWORD GetMidiMessage()
    {
        if(startpos == endpos)  // check for buffer empty
            return -1;
        DWORD msg = stream[startpos][0];
        startpos++;
        if(startpos == maxPos)  // check for buffer rolloff
            startpos = 0;
        return msg;
    }

    DWORD PeekMessageTime()
    {
        if(startpos == endpos)  // check for buffer empty
            return (DWORD) -1;
        return stream[startpos][1];
    }

    DWORD PeekMessageTimeAt(unsigned int pos)
    {
        if(startpos == endpos)  // check for buffer empty
            return -1;
        unsigned int peekPos = (startpos + pos) % maxPos;
        return stream[peekPos][1];
    }

    void Clean()
    {
        startpos = 0;
        endpos = 0;
        memset(stream, 0, sizeof(stream));
    }

} midiStream;

static class SynthEventWin32
{
private:
    HANDLE hEvent;

public:
    int Init()
    {
        hEvent = CreateEvent(NULL, false, true, NULL);
        if(hEvent == NULL)
        {
            MessageBoxW(NULL, L"Can't create sync object", L"libADLMIDI", MB_OK | MB_ICONEXCLAMATION);
            return 1;
        }
        return 0;
    }

    void Close()
    {
        CloseHandle(hEvent);
    }

    void Wait()
    {
        WaitForSingleObject(hEvent, INFINITE);
    }

    void Release()
    {
        SetEvent(hEvent);
    }
} synthEvent;

static class WaveOutWin32
{
private:
    HWAVEOUT    hWaveOut;
    WAVEHDR     *WaveHdr;
    HANDLE      hEvent;
    DWORD       chunks;
    DWORD       prevPlayPos;
    DWORD       getPosWraps;
    bool        stopProcessing;
    WORD        formatType;
    DWORD       sizeSample;

public:
    WaveOutWin32() :
        hWaveOut(NULL),
        WaveHdr(NULL),
        hEvent(NULL),
        chunks(0),
        prevPlayPos(0),
        getPosWraps(0),
        stopProcessing(false),
        formatType(0),
        sizeSample(0)
    {}

    int Init(ADL_UInt8 *buffer, size_t bufferSize,
             WORD formatTag,
             unsigned int chunkSize, bool useRingBuffer,
             unsigned int sampleRate, UINT outDevice)
    {
        DWORD callbackType = CALLBACK_NULL;
        DWORD_PTR callback = (DWORD_PTR)NULL;
        size_t numFrames;

        if(hWaveOut)
            Close();

        formatType = formatTag;
        sizeSample = formatType == WAVE_FORMAT_IEEE_FLOAT ? sizeof(float) : sizeof(Bit16s);
        numFrames = bufferSize / (sizeSample * s_audioChannels);

        if(!useRingBuffer)
        {
            hEvent = CreateEvent(NULL, false, true, NULL);
            callback = (DWORD_PTR)hEvent;
            callbackType = CALLBACK_EVENT;
        }

        PCMWAVEFORMAT wFormat =
        {
            formatType, s_audioChannels,
            sampleRate,
            (DWORD)(sampleRate * sizeSample * s_audioChannels),
            (WORD)(s_audioChannels * sizeSample),
            (WORD)(8 * sizeSample)
        };

        // Open waveout device
        int wResult = waveOutOpen(&hWaveOut, outDevice, (LPWAVEFORMATEX)&wFormat,
                                  callback, (DWORD_PTR)&midiSynth, callbackType);

        if(wResult != MMSYSERR_NOERROR)
        {
            if(formatType != WAVE_FORMAT_IEEE_FLOAT)
                MessageBoxW(NULL, L"Failed to open waveform output device", L"libADLMIDI", MB_OK | MB_ICONEXCLAMATION);

            Close();
            return 2;
        }

        // Prepare headers
        chunks = useRingBuffer ? 1 : numFrames / chunkSize;
        WaveHdr = new WAVEHDR[chunks];

        LPSTR chunkStart = (LPSTR)buffer;
        DWORD chunkBytes = s_audioChannels * sizeSample * chunkSize;

        for(UINT i = 0; i < chunks; i++)
        {
            if(useRingBuffer)
            {
                WaveHdr[i].dwBufferLength = bufferSize;
                WaveHdr[i].lpData = chunkStart;
                WaveHdr[i].dwFlags = WHDR_BEGINLOOP | WHDR_ENDLOOP;
                WaveHdr[i].dwLoops = -1L;
            }
            else
            {
                WaveHdr[i].dwBufferLength = chunkBytes;
                WaveHdr[i].lpData = chunkStart;
                WaveHdr[i].dwFlags = 0L;
                WaveHdr[i].dwLoops = 0L;
                chunkStart += chunkBytes;
            }

            wResult = waveOutPrepareHeader(hWaveOut, &WaveHdr[i], sizeof(WAVEHDR));
            if(wResult != MMSYSERR_NOERROR)
            {
                MessageBoxW(NULL, L"Failed to Prepare Header", L"libADLMIDI", MB_OK | MB_ICONEXCLAMATION);
                return 3;
            }
        }

        stopProcessing = false;
        return 0;
    }

    int Close()
    {
        int wResult;

        stopProcessing = true;
        if(hEvent != NULL)
            SetEvent(hEvent);

        if(hWaveOut != NULL)
        {
            wResult = waveOutReset(hWaveOut);
            if(wResult != MMSYSERR_NOERROR)
            {
                MessageBoxW(NULL, L"Failed to Reset WaveOut", L"libADLMIDI", MB_OK | MB_ICONEXCLAMATION);
                return 8;
            }
        }

        for(UINT i = 0; i < chunks; i++)
        {
            wResult = waveOutUnprepareHeader(hWaveOut, &WaveHdr[i], sizeof(WAVEHDR));
            if(wResult != MMSYSERR_NOERROR)
            {
                MessageBoxW(NULL, L"Failed to Unprepare Wave Header", L"libADLMIDI", MB_OK | MB_ICONEXCLAMATION);
                return 8;
            }
        }

        if(WaveHdr != NULL)
            delete[] WaveHdr;
        WaveHdr = NULL;

        if(hWaveOut != NULL)
        {
            wResult = waveOutClose(hWaveOut);
            if(wResult != MMSYSERR_NOERROR)
            {
                MessageBoxW(NULL, L"Failed to Close WaveOut", L"libADLMIDI", MB_OK | MB_ICONEXCLAMATION);
                return 8;
            }
        }

        hWaveOut = NULL;

        if(hEvent != NULL)
        {
            CloseHandle(hEvent);
            hEvent = NULL;
        }

        return 0;
    }

    int Start()
    {
        getPosWraps = 0;
        prevPlayPos = 0;
        for(UINT i = 0; i < chunks; i++)
        {
            if(waveOutWrite(hWaveOut, &WaveHdr[i], sizeof(WAVEHDR)) != MMSYSERR_NOERROR)
            {
                MessageBoxW(NULL, L"Failed to write block to device", L"libADLMIDI", MB_OK | MB_ICONEXCLAMATION);
                return 4;
            }
        }

        _beginthread(RenderingThread, 8192 * sizeSample, this);
        return 0;
    }

    int Pause()
    {
        if(waveOutPause(hWaveOut) != MMSYSERR_NOERROR)
        {
            MessageBoxW(NULL, L"Failed to Pause wave playback", L"libADLMIDI", MB_OK | MB_ICONEXCLAMATION);
            return 9;
        }
        return 0;
    }

    int Resume()
    {
        if(waveOutRestart(hWaveOut) != MMSYSERR_NOERROR)
        {
            MessageBoxW(NULL, L"Failed to Resume wave playback", L"libADLMIDI", MB_OK | MB_ICONEXCLAMATION);
            return 9;
        }
        return 0;
    }

    UINT64 GetPos()
    {
        MMTIME mmTime;
        mmTime.wType = TIME_SAMPLES;

        if(waveOutGetPosition(hWaveOut, &mmTime, sizeof(MMTIME)) != MMSYSERR_NOERROR)
        {
            MessageBoxW(NULL, L"Failed to get current playback position", L"libADLMIDI", MB_OK | MB_ICONEXCLAMATION);
            return 10;
        }

        if(mmTime.wType != TIME_SAMPLES)
        {
            MessageBoxW(NULL, L"Failed to get # of samples played", L"libADLMIDI", MB_OK | MB_ICONEXCLAMATION);
            return 10;
        }

        // Deal with waveOutGetPosition() wraparound. For 16-bit stereo output, it equals 2^27,
        // presumably caused by the internal 32-bit counter of bits played.
        // The output of that nasty waveOutGetPosition() isn't monotonically increasing
        // even during 2^27 samples playback, so we have to ensure the difference is big enough...
        int delta = mmTime.u.sample - prevPlayPos;
        if(delta < -(1 << 26))
        {
            std::cout << "OPL3: GetPos() wrap: " << delta << "\n";
            ++getPosWraps;
        }

        prevPlayPos = mmTime.u.sample;
        return mmTime.u.sample + getPosWraps * (1 << 27);
    }

    static void RenderingThread(void *);
} s_waveOut;

void WaveOutWin32::RenderingThread(void *)
{
    if(s_waveOut.chunks == 1)
    {
        // Rendering using single looped ring buffer
        while(!s_waveOut.stopProcessing)
            midiSynth.RenderAvailableSpace();
    }
    else
    {
        while(!s_waveOut.stopProcessing)
        {
            bool allBuffersRendered = true;

            for(UINT i = 0; i < s_waveOut.chunks; i++)
            {
                if(s_waveOut.WaveHdr[i].dwFlags & WHDR_DONE)
                {
                    allBuffersRendered = false;
                    midiSynth.Render((Bit8u *)s_waveOut.WaveHdr[i].lpData,
                                     s_waveOut.WaveHdr[i].dwBufferLength);

                    if(waveOutWrite(s_waveOut.hWaveOut, &s_waveOut.WaveHdr[i], sizeof(WAVEHDR)) != MMSYSERR_NOERROR)
                        MessageBoxW(NULL, L"Failed to write block to device", L"libADLMIDI", MB_OK | MB_ICONEXCLAMATION);

                    midiSynth.CheckForSignals();
                }
            }

            if(allBuffersRendered)
                WaitForSingleObject(s_waveOut.hEvent, INFINITE);
        }
    }
}


MidiSynth::MidiSynth() :
    buffer(NULL),
    bufferSizeB(0),
    synth(NULL)
{
    m_setupInit = false;
    useRingBuffer = false;
    volumeFactorL = 1.0f;
    volumeFactorR = 1.0f;
    gain = 1.0f;

    setupDefault(&m_setup);
    loadSetup();
    ::openSignalListener();
}

MidiSynth::~MidiSynth()
{
    if(synth)
        adl_close(synth);
    synth = NULL;
    ::closeSignalListener();
}

MidiSynth &MidiSynth::getInstance()
{
    static MidiSynth *instance = new MidiSynth;
    return *instance;
}

// Renders all the available space in the single looped ring buffer
void MidiSynth::RenderAvailableSpace()
{
    DWORD playPos = s_waveOut.GetPos() % bufferSize;
    DWORD framesToRender;

    if(playPos < framesRendered)
    {
        // Buffer wrap, render 'till the end of the buffer
        framesToRender = bufferSize - framesRendered;
    }
    else
    {
        framesToRender = playPos - framesRendered;
        if(framesToRender < chunkSize)
        {
            Sleep(1 + (chunkSize - framesToRender) * 1000 / sampleRate);
            return;
        }
    }

    midiSynth.Render(buffer + (synthAudioFormat.containerSize * framesRendered), framesToRender);
}

// Renders totalFrames frames starting from bufpos
// The number of frames rendered is added to the global counter framesRendered
void MidiSynth::Render(Bit8u *bufpos_p, DWORD bufSize)
{
    DWORD totalFrames = bufSize / (synthAudioFormat.containerSize * s_audioChannels);

    while(totalFrames > 0)
    {
        DWORD timeStamp;
        // Incoming MIDI messages timestamped with the current audio playback position + midiLatency
        while((timeStamp = midiStream.PeekMessageTime()) == framesRendered)
        {
            DWORD msg = midiStream.GetMidiMessage();

            synthEvent.Wait();

            Bit8u event = msg & 0xFF;
            Bit8u channel = msg & 0x0F;
            Bit8u p1 = (msg >> 8) & 0x7f;
            Bit8u p2 = (msg >> 16) & 0x7f;

            event &= 0xF0;

            if(event == 0xF0)
            {
                switch (channel)
                {
                case 0xF:
                    adl_reset(synth);
                    break;
                }
            }

            switch(event & 0xF0)
            {
            case 0x80:
                adl_rt_noteOff(synth, channel, p1);
                break;
            case 0x90:
                adl_rt_noteOn(synth, channel, p1, p2);
                break;
            case 0xA0:
                adl_rt_noteAfterTouch(synth, channel, p1, p2);
                break;
            case 0xB0:
                adl_rt_controllerChange(synth, channel, p1, p2);
                break;
            case 0xC0:
                adl_rt_patchChange(synth, channel, p1);
                break;
            case 0xD0:
                adl_rt_channelAfterTouch(synth, channel, p1);
                break;
            case 0xE0:
                adl_rt_pitchBendML(synth, channel, p2, p1);
                break;
            }

            synthEvent.Release();
        }

        // Find out how many frames to render. The value of timeStamp == -1 indicates the MIDI buffer is empty
        DWORD framesToRender = timeStamp - framesRendered;
        if(framesToRender > totalFrames)
        {
            // MIDI message is too far - render the rest of frames
            framesToRender = totalFrames;
        }

        synthEvent.Wait();
        adl_generateFormat(synth, framesToRender * s_audioChannels,
                           bufpos_p, bufpos_p + synthAudioFormat.containerSize,
                           &synthAudioFormat);

        // Apply the volume
        float g_l = volumeFactorL * gain;
        float g_r = volumeFactorR * gain;

        if(synthAudioFormat.type == ADLMIDI_SampleType_F32)
        {
            float *bufpos = (float *)bufpos_p;
            for(size_t i = 0; i < framesToRender * s_audioChannels; i += s_audioChannels)
            {
                bufpos[i + 0] *= g_l;
                bufpos[i + 1] *= g_r;
            }
        }
        else
        {
            Bit16s *bufpos = (Bit16s *)bufpos_p;
            for(size_t i = 0; i < framesToRender * s_audioChannels; i += s_audioChannels)
            {
                bufpos[i + 0] *= g_l;
                bufpos[i + 1] *= g_r;
            }
        }

        synthEvent.Release();
        framesRendered += framesToRender;
        // each frame consists of two samples for both the Left and Right channels
        bufpos_p += s_audioChannels * framesToRender * synthAudioFormat.containerSize;
        totalFrames -= framesToRender;
    }

    // Wrap framesRendered counter
    if(framesRendered >= bufferSize)
        framesRendered -= bufferSize;
}

void MidiSynth::CheckForSignals()
{
    int cmd = ::hasReloadSetupSignal();

    if(cmd == 0)
        return;

    switch(cmd)
    {
    case DRV_SIGNAL_RELOAD_SETUP: // Reload settings on the fly
        this->loadSetup();
        LoadSynthSetup();
        break;

    case DRV_SIGNAL_RESET_SYNTH:
        adl_reset(synth);
        break;

    case DRV_SIGNAL_UPDATE_GAIN:
        this->loadGain();
        break;

    default:
        break;
    }

    if(cmd > 0)
        ::resetSignal();
}

unsigned int MidiSynth::MillisToFrames(unsigned int millis)
{
    return UINT(sampleRate * millis / 1000.f);
}

void MidiSynth::LoadSettings()
{
    sampleRate = 49716;
    bufferSize = MillisToFrames(100);
    chunkSize = MillisToFrames(10);
    midiLatency = MillisToFrames(0);

    if(!useRingBuffer)
    {
        // Number of chunks should be ceil(bufferSize / chunkSize)
        DWORD chunks = (bufferSize + chunkSize - 1) / chunkSize;
        // Refine bufferSize as chunkSize * number of chunks, no less then the specified value
        bufferSize = chunks * chunkSize;
    }
}

int MidiSynth::Init()
{
    LoadSettings();

    if(!buffer)
    {
        bufferSizeB = s_audioChannels * bufferSize * sizeof(float);
        buffer = (ADL_UInt8 *)malloc(bufferSizeB); // each frame consists of two samples for both the Left and Right channels
    }

    // Init synth
    if(synthEvent.Init())
        return 1;

    synth = adl_init(49716);
    if(!synth)
    {
        MessageBoxW(NULL, L"Can't open Synth", L"libADLMIDI", MB_OK | MB_ICONEXCLAMATION);
        return 1;
    }

    synthAudioFormat.type = ADLMIDI_SampleType_F32;
    synthAudioFormat.sampleOffset = s_audioChannels * sizeof(float);
    synthAudioFormat.containerSize = sizeof(float);

    m_setupInit = false;
    LoadSynthSetup();

    UINT wResult = s_waveOut.Init(buffer, bufferSizeB,
                                  WAVE_FORMAT_IEEE_FLOAT,
                                  chunkSize, useRingBuffer, sampleRate, m_setup.outputDevice);

    if(wResult)
    {
        synthAudioFormat.type = ADLMIDI_SampleType_S16;
        synthAudioFormat.sampleOffset = s_audioChannels * sizeof(Bit16s);
        synthAudioFormat.containerSize = sizeof(Bit16s);

        bufferSizeB = s_audioChannels * bufferSize * sizeof(Bit16s);
        buffer = (ADL_UInt8 *)realloc(buffer, bufferSizeB); // Shrink the buffer
        wResult = s_waveOut.Init(buffer, bufferSizeB,
                                         WAVE_FORMAT_PCM,
                                         chunkSize, useRingBuffer, sampleRate, m_setup.outputDevice);
    }

    if(wResult)
        return wResult;

    m_setupCurrent.outputDevice = m_setup.outputDevice;

    // Start playing stream
    adl_generateFormat(synth, bufferSize * s_audioChannels,
                       buffer,
                       buffer + synthAudioFormat.containerSize,
                       &synthAudioFormat);
    framesRendered = 0;

    wResult = s_waveOut.Start();
    return wResult;
}

int MidiSynth::Reset()
{
#ifdef DRIVER_MODE
    return 0;
#endif

    UINT wResult = s_waveOut.Pause();
    if(wResult) return wResult;

    synthEvent.Wait();

    if(synth)
        adl_close(synth);

    synth = adl_init(49716);
    if(!synth)
    {
        MessageBoxW(NULL, L"Can't open Synth", L"libADLMIDI", MB_OK | MB_ICONEXCLAMATION);
        return 1;
    }

    m_setupInit = false;
    LoadSynthSetup();

    synthEvent.Release();

    wResult = s_waveOut.Resume();
    return wResult;
}

void MidiSynth::ResetSynth()
{
    synthEvent.Wait();
    adl_reset(synth);
    midiStream.Clean();
    synthEvent.Release();
}

void MidiSynth::PanicSynth()
{
    synthEvent.Wait();
    adl_panic(synth);
    synthEvent.Release();
}

void MidiSynth::PushMIDI(DWORD msg)
{
    midiStream.PutMessage(msg, (s_waveOut.GetPos() + midiLatency) % bufferSize);
}

void MidiSynth::PlaySysex(Bit8u *bufpos, DWORD len)
{
    synthEvent.Wait();
    adl_rt_systemExclusive(synth, bufpos, len);
    synthEvent.Release();
}

void MidiSynth::SetVolume(DWORD vol)
{
    volumeFactorR = (float)0xFFFF / HIWORD(vol);
    volumeFactorL = (float)0xFFFF / LOWORD(vol);
}

DWORD MidiSynth::GetVolume()
{
    return MAKELONG((DWORD)(0xFFFF * volumeFactorL), (DWORD)(0xFFFF * volumeFactorR));
}

void MidiSynth::loadSetup()
{
    ::loadSetup(&m_setup);
    gain = (float)m_setup.gain100 / 100.f;
}

void MidiSynth::loadGain()
{
    ::getGain(&m_setup);
    gain = (float)m_setup.gain100 / 100.f;
}

void MidiSynth::LoadSynthSetup()
{
    if(!m_setupInit || m_setupCurrent.emulatorId != m_setup.emulatorId)
    {
        adl_switchEmulator(synth, m_setup.emulatorId);
        m_setupCurrent.emulatorId = m_setup.emulatorId;
    }

    if(!m_setupInit || m_setupCurrent.numChips != m_setup.numChips)
    {
        adl_setNumChips(synth, m_setup.numChips);
        m_setupCurrent.numChips = m_setup.numChips;
    }

    if(!m_setupInit || m_setupCurrent.flagDeepTremolo != m_setup.flagDeepTremolo)
    {
        switch(m_setup.flagDeepTremolo)
        {
        case BST_INDETERMINATE:
            adl_setHTremolo(synth, -1);
            break;
        case BST_CHECKED:
            adl_setHTremolo(synth, 1);
            break;
        case BST_UNCHECKED:
            adl_setHTremolo(synth, 0);
            break;
        }
        m_setupCurrent.flagDeepTremolo = m_setup.flagDeepTremolo;
    }

    if(!m_setupInit || m_setupCurrent.flagDeepVibrato != m_setup.flagDeepVibrato)
    {
        switch(m_setup.flagDeepVibrato)
        {
        case BST_INDETERMINATE:
            adl_setHVibrato(synth, -1);
            break;
        case BST_CHECKED:
            adl_setHVibrato(synth, 1);
            break;
        case BST_UNCHECKED:
            adl_setHVibrato(synth, 0);
            break;
        }
        m_setupCurrent.flagDeepVibrato = m_setup.flagDeepVibrato;
    }

    if(!m_setupInit || m_setupCurrent.flagSoftPanning != m_setup.flagSoftPanning)
    {
        adl_setSoftPanEnabled(synth, m_setup.flagSoftPanning);
        m_setupCurrent.flagSoftPanning = m_setup.flagSoftPanning;
    }


    if(!m_setupInit || m_setupCurrent.flagScaleModulators != m_setup.flagScaleModulators)
    {
        adl_setScaleModulators(synth, m_setup.flagScaleModulators);
        m_setupCurrent.flagScaleModulators = m_setup.flagScaleModulators;
    }

    if(!m_setupInit || m_setupCurrent.flagFullBrightness != m_setup.flagFullBrightness)
    {
        adl_setFullRangeBrightness(synth, m_setup.flagFullBrightness);
        m_setupCurrent.flagFullBrightness = m_setup.flagFullBrightness;
    }

    if(!m_setupInit || m_setupCurrent.volumeModel != m_setup.volumeModel)
    {
        adl_setVolumeRangeModel(synth, m_setup.volumeModel);
        m_setupCurrent.volumeModel = m_setup.volumeModel;
    }

    if(!m_setupInit || m_setupCurrent.chanAlloc != m_setup.chanAlloc)
    {
        adl_setChannelAllocMode(synth, m_setup.chanAlloc);
        m_setupCurrent.chanAlloc = m_setup.chanAlloc;
    }

    if(!m_setupInit || m_setupCurrent.numChips != m_setup.numChips)
    {
        adl_setNumChips(synth, m_setup.numChips);
        m_setupCurrent.numChips = m_setup.numChips;
    }

    if(!m_setupInit || m_setupCurrent.num4ops != m_setup.num4ops)
    {
        adl_setNumFourOpsChn(synth, m_setup.num4ops);
        m_setupCurrent.num4ops = m_setup.num4ops;
    }

    if(!m_setupInit ||
       m_setupCurrent.useExternalBank != m_setup.useExternalBank ||
       m_setupCurrent.bankId != m_setup.bankId ||
       wcscmp(m_setupCurrent.bankPath, m_setup.bankPath) != 0
    )
    {
        if(m_setup.useExternalBank)
        {
            char pathUtf8[MAX_PATH * 4];
            ZeroMemory(pathUtf8, MAX_PATH * 4);
            int len = WideCharToMultiByte(CP_UTF8, 0, m_setup.bankPath, wcslen(m_setup.bankPath), pathUtf8, MAX_PATH * 4, 0, 0);
            pathUtf8[len] = '\0';
            adl_openBankFile(synth, pathUtf8);
        }
        else
            adl_setBank(synth, m_setup.bankId);

        m_setupCurrent.useExternalBank = m_setup.useExternalBank;
        m_setupCurrent.bankId = m_setup.bankId;
        wcscpy(m_setupCurrent.bankPath, m_setup.bankPath);
    }

    m_setupInit = true;
}

void MidiSynth::Close()
{
    s_waveOut.Pause();
    s_waveOut.Close();
    synthEvent.Wait();
    //synth->close();

    // Cleanup memory
    if(synth)
        adl_close(synth);
    synth = NULL;

    if(buffer)
        free(buffer);
    buffer = NULL;

    synthEvent.Close();
}

}