DWinProgramming /Samples/Chap22/Drum/DrumTime.d

Language D Lines 190
MD5 Hash 882edbd524d8a00c66afc643dee77de1
Repository git://github.com/AndrejMitrovic/DWinProgramming.git View Raw File View Project SPDX
  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
/+
 + Copyright (c) Charles Petzold, 1998.
 + Ported to the D Programming Language by Andrej Mitrovic, 2011.
 +/

module DrumTime;

import core.memory;
import core.runtime;
import core.thread;
import std.algorithm : min, max;

import std.conv;
import std.math;
import std.range;
import std.string;
import std.stdio;
import std.utf;

auto toUTF16z(S)(S s)
{
    return toUTFz!(const(wchar)*)(s);
}

pragma(lib, "gdi32.lib");
pragma(lib, "comdlg32.lib");
pragma(lib, "winmm.lib");
import win32.windef;
import win32.winuser;
import win32.wingdi;
import win32.winbase;
import win32.commdlg;
import win32.mmsystem;

enum NUM_PERC         = 47;
enum WM_USER_NOTIFY   = (WM_USER + 1);
enum WM_USER_FINISHED = (WM_USER + 2);
enum WM_USER_ERROR    = (WM_USER + 3);

align(2) struct DRUM
{
    short iMsecPerBeat;
    short iVelocity;
    short iNumBeats;
    DWORD[NUM_PERC] dwSeqPerc;
    DWORD[NUM_PERC] dwSeqPian;
}
alias DRUM* PDRUM;

auto minmax(T1, T2, T3) (T1 a, T2 x, T3 b)
{
    return min(max(x, a), b);
}

enum TIMER_RES = 5;

__gshared BOOL bSequenceGoing, bEndSequence;
__gshared DRUM drum;
__gshared HMIDIOUT hMidiOut;
__gshared HWND hwndNotify;
__gshared int  iIndex;
__gshared UINT uTimerRes, uTimerID;

DWORD MidiOutMessage(HMIDIOUT hMidi, int iStatus, int iChannel, int iData1, int iData2)
{
    DWORD dwMessage;
    dwMessage = iStatus | iChannel | (iData1 << 8) | (iData2 << 16);
    return midiOutShortMsg(hMidi, dwMessage);
}

void DrumSetParams(PDRUM pdrum)
{
    drum = *pdrum;
}

BOOL DrumBeginSequence(HWND hwnd)
{
    TIMECAPS tc;

    hwndNotify = hwnd;             // Save window handle for notification
    DrumEndSequence(TRUE);         // Stop current sequence if running

    // Open the MIDI Mapper output port
    if (midiOutOpen(&hMidiOut, MIDIMAPPER, 0, 0, 0))
        return FALSE;

    // Send Program Change messages for channels 9 and 0
    MidiOutMessage(hMidiOut, 0xC0, 9, 0, 0);
    MidiOutMessage(hMidiOut, 0xC0, 0, 0, 0);

    // Begin sequence by setting a timer event
    timeGetDevCaps(&tc, TIMECAPS.sizeof);
    uTimerRes = minmax(tc.wPeriodMin, TIMER_RES, tc.wPeriodMax);
    timeBeginPeriod(uTimerRes);

    uTimerID = timeSetEvent(max(cast(UINT)uTimerRes, cast(UINT)drum.iMsecPerBeat),
                            uTimerRes, &DrumTimerFunc, 0, TIME_ONESHOT);

    if (uTimerID == 0)
    {
        timeEndPeriod(uTimerRes);
        midiOutClose(hMidiOut);
        return FALSE;
    }

    iIndex         = -1;
    bEndSequence   = FALSE;
    bSequenceGoing = TRUE;

    return TRUE;
}

void DrumEndSequence(BOOL bRightAway)
{
    if (bRightAway)
    {
        if (bSequenceGoing)
        {
            // stop the timer
            if (uTimerID)
                timeKillEvent(uTimerID);

            timeEndPeriod(uTimerRes);

            // turn off all notes
            MidiOutMessage(hMidiOut, 0xB0, 9, 123, 0);
            MidiOutMessage(hMidiOut, 0xB0, 0, 123, 0);

            // close the MIDI port
            midiOutClose(hMidiOut);
            bSequenceGoing = FALSE;
        }
    }
    else
        bEndSequence = TRUE;
}

extern (Windows)
void DrumTimerFunc(UINT uID, UINT uMsg, DWORD dwUser, DWORD dw1, DWORD dw2)
{
    static DWORD[NUM_PERC] dwSeqPercLast, dwSeqPianLast;
    int i;

    // Note Off messages for channels 9 and 0
    if (iIndex != -1)
    {
        for (i = 0; i < NUM_PERC; i++)
        {
            if (dwSeqPercLast[i] & 1 << iIndex)
                MidiOutMessage(hMidiOut, 0x80, 9, i + 35, 0);

            if (dwSeqPianLast[i] & 1 << iIndex)
                MidiOutMessage(hMidiOut, 0x80, 0, i + 35, 0);
        }
    }

    // Increment index and notify window to advance bouncing ball
    iIndex = (iIndex + 1) % drum.iNumBeats;
    PostMessage(hwndNotify, WM_USER_NOTIFY, iIndex, timeGetTime());

    // Check if ending the sequence
    if (bEndSequence && iIndex == 0)
    {
        PostMessage(hwndNotify, WM_USER_FINISHED, 0, 0L);
        return;
    }

    // Note On messages for channels 9 and 0
    for (i = 0; i < NUM_PERC; i++)
    {
        if (drum.dwSeqPerc[i] & 1 << iIndex)
            MidiOutMessage(hMidiOut, 0x90, 9, i + 35, drum.iVelocity);

        if (drum.dwSeqPian[i] & 1 << iIndex)
            MidiOutMessage(hMidiOut, 0x90, 0, i + 35, drum.iVelocity);

        dwSeqPercLast[i] = drum.dwSeqPerc[i];
        dwSeqPianLast[i] = drum.dwSeqPian[i];
    }

    // Set a new timer event
    uTimerID = timeSetEvent(max(cast(int)uTimerRes, drum.iMsecPerBeat),
                            uTimerRes, &DrumTimerFunc, 0, TIME_ONESHOT);

    if (uTimerID == 0)
    {
        PostMessage(hwndNotify, WM_USER_ERROR, 0, 0);
    }
}
Back to Top