Source

doom / src / win32 / win_cd.c

Full commit
  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
// Emacs style mode select   -*- C++ -*- 
//-----------------------------------------------------------------------------
//
// $Id: win_cd.c 538 2009-09-23 23:24:07Z smite-meister $
//
// Copyright (C) 1998-2000 by DooM Legacy Team.
//
// This program is free software; you can redistribute it and/or
// modify it under the terms of the GNU General Public License
// as published by the Free Software Foundation; either version 2
// 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 General Public License for more details.
//
//
// $Log: win_cd.c,v $
// Revision 1.3  2000/09/01 19:34:38  bpereira
// no message
//
// Revision 1.2  2000/02/27 00:42:12  hurdler
// fix CR+LF problem
//
// Revision 1.1.1.1  2000/02/22 20:32:33  hurdler
// Initial import into CVS (v1.29 pr3)
//
//
// DESCRIPTION:
//      cd music interface (uses MCI).
//
//-----------------------------------------------------------------------------

#include "../doomdef.h"
#include "win_main.h"
#include <mmsystem.h>

#include "../command.h"
#include "../doomtype.h"
#include "../i_sound.h"
#include "../i_system.h"

#include "../s_sound.h"

#define MAX_CD_TRACKS       100

typedef struct {
    BOOL    IsAudio;
    DWORD   Start, End;
    DWORD   Length;         // minutes
} CDTrack;

// -------
// private
// -------
static  CDTrack             m_nTracks[MAX_CD_TRACKS];
static  int                             m_nTracksCount;             // up to MAX_CD_TRACKS
static  MCI_STATUS_PARMS        m_MCIStatus;
static  MCI_OPEN_PARMS          m_MCIOpen;

// ------
// protos
// ------
void Command_Cd_f (void);


// -------------------
// MCIErrorMessageBox
// Retrieve error message corresponding to return value from
//  mciSendCommand() or mciSenString()
// -------------------
static void MCIErrorMessageBox (MCIERROR iErrorCode)
{
    char szErrorText[128];
    if (!mciGetErrorString (iErrorCode, szErrorText, sizeof(szErrorText)))
        wsprintf(szErrorText,"MCI CD Audio Unknow Error #%d\n", iErrorCode);
    CONS_Printf (szErrorText);
    /*MessageBox (GetActiveWindow(), szTemp+1, "LEGACY",
                MB_OK | MB_ICONSTOP );*/
}


// --------
// CD_Reset
// --------
static void CD_Reset (void)
{
    // no win32 equivalent
    //faB: for DOS, some odd drivers like to be reset sometimes.. useless in MCI I guess
}


// ----------------
// CD_ReadTrackInfo
// Read in number of tracks, and length of each track in minutes/seconds
// returns true if error
// ----------------
static BOOL CD_ReadTrackInfo (void)
{
        int         i;
    int         nTrackLength;
    MCIERROR    iErr;
    
        m_nTracksCount = 0;
                
        m_MCIStatus.dwItem = MCI_STATUS_NUMBER_OF_TRACKS;
        if ( (iErr = mciSendCommand(m_MCIOpen.wDeviceID, MCI_STATUS, MCI_STATUS_ITEM|MCI_WAIT, (DWORD)(LPVOID)&m_MCIStatus)) )
        {
                MCIErrorMessageBox (iErr);
                return FALSE;
        }
        m_nTracksCount = m_MCIStatus.dwReturn;
        if (m_nTracksCount > MAX_CD_TRACKS)
                m_nTracksCount = MAX_CD_TRACKS;

        for (i=0; i<m_nTracksCount; i++)
        {
                m_MCIStatus.dwTrack = i+1;
        m_MCIStatus.dwItem = MCI_STATUS_LENGTH;
                if ((iErr = mciSendCommand(m_MCIOpen.wDeviceID, MCI_STATUS, MCI_TRACK|MCI_STATUS_ITEM|MCI_WAIT, (DWORD)(LPVOID)&m_MCIStatus)))
        {
            MCIErrorMessageBox (iErr);
            return FALSE;
        }
                nTrackLength = (DWORD)(MCI_MSF_MINUTE(m_MCIStatus.dwReturn)*60 + MCI_MSF_SECOND(m_MCIStatus.dwReturn));
                m_nTracks[i].Length = nTrackLength;

        m_MCIStatus.dwItem = MCI_CDA_STATUS_TYPE_TRACK;
        if ((iErr = mciSendCommand(m_MCIOpen.wDeviceID, MCI_STATUS, MCI_TRACK|MCI_STATUS_ITEM|MCI_WAIT, (DWORD)(LPVOID)&m_MCIStatus)))
        {
            MCIErrorMessageBox (iErr);
            return FALSE;
        }
        m_nTracks[i].IsAudio = (m_MCIStatus.dwReturn == MCI_CDA_TRACK_AUDIO);
        }
        
        return TRUE;
}


// ------------
// CD_TotalTime
// returns total time for all audio tracks in seconds
// ------------
static int CD_TotalTime (void)
{
        int nTotalLength = 0;
        int nTrack;
    for (nTrack=0; nTrack<m_nTracksCount; nTrack++) {
        if (m_nTracks[nTrack].IsAudio)
                    nTotalLength = nTotalLength + m_nTracks[nTrack].Length;
    }
        return nTotalLength;
}


//======================================================================
//                   CD AUDIO MUSIC SUBSYSTEM
//======================================================================

byte   cdaudio_started=0;   // for system startup/shutdown

static boolean cdPlaying = false;
static int     cdPlayTrack;         // when cdPlaying is true
static boolean cdLooping = false;
static byte    cdRemap[MAX_CD_TRACKS];
static boolean cdEnabled=true;      // cd info available
static boolean cdValid;             // true when last cd audio info was ok
static boolean wasPlaying;
static int     cdVolume=0;          // current cd volume (0-31)

// 0-31 like Music & Sfx, though CD hardware volume is 0-255.
consvar_t   cd_volume = {"cd_volume","31",CV_SAVE,soundvolume_cons_t};

// allow Update for next/loop track
// some crap cd drivers take up to
// a second for a simple 'busy' check..
// (on those Update can be disabled)
consvar_t   cdUpdate  = {"cd_update","1",CV_SAVE};

// hour,minutes,seconds
static char* hms(int seconds)
{
    int hours, minutes;
    static char s[9];

    minutes = seconds / 60;
    seconds %= 60;
    hours = minutes / 60;
    minutes %= 60;
    if (hours>0)
        sprintf (s, "%d:%02d:%02d", hours, minutes, seconds);
    else
        sprintf (s, "%2d:%02d", minutes, seconds);
    return s;
}

void Command_Cd_f (void)
{
    char*     s;
    int       i,j;

    if (!cdaudio_started)
        return;

    if (COM_Argc()<2)
    {
        CONS_Printf ("cd [on] [off] [remap] [reset] [open]\n"
                     "   [info] [play <track>] [loop <track>]\n"
                     "   [stop] [resume]\n");
        return;
    }

    s = COM_Argv(1);

    // activate cd music
    if (!strncmp(s,"on",2))
    {
        cdEnabled = true;
        return;
    }

    // stop/deactivate cd music
    if (!strncmp(s,"off",3))
    {
        if (cdPlaying)
            I_StopCD ();
        cdEnabled = false;
        return;
    }

    // remap tracks
    if (!strncmp(s,"remap",5))
    {
        i = COM_Argc() - 2;
        if (i <= 0)
        {
            CONS_Printf ("CD tracks remapped in that order :\n");
            for (j = 1; j < MAX_CD_TRACKS; j++)
                if (cdRemap[j] != j)
                    CONS_Printf (" %2d -> %2d\n", j, cdRemap[j]);
            return;
        }
        for (j = 1; j <= i; j++)
            cdRemap[j] = atoi (COM_Argv (j+1));
        return;
    }

    // reset the CD driver, useful on some odd cd's
    if (!strncmp(s,"reset",5))
    {
        cdEnabled = true;
        if (cdPlaying)
            I_StopCD ();
        for (i = 0; i < MAX_CD_TRACKS; i++)
            cdRemap[i] = i;
        CD_Reset();
        cdValid = CD_ReadTrackInfo();
        return;
    }

    // any other command is not allowed until we could retrieve cd information
    if (!cdValid)
    {
        CONS_Printf ("CD is not ready.\n");
        return;
    }

    /* faB: not with MCI, didn't find it, useless anyway
    if (!strncmp(s,"open",4))
    {
        if (cdPlaying)
            I_StopCD ();
        bcd_open_door();
        cdValid = false;
        return;
    }*/

    if (!strncmp(s,"info",4))
    {
        if (!CD_ReadTrackInfo()) {
            cdValid = false;
            return;
        }

        cdValid = true;

        if (m_nTracksCount <= 0)
            CONS_Printf ("No audio tracks\n");
        else
        {
            // display list of tracks
            // highlight current playing track
            for (i = 0; i<m_nTracksCount; i++)
            {
                CONS_Printf    ("%s%2d. %s  %s\n",
                                cdPlaying && (cdPlayTrack == i) ? "\2 " : " ",
                                i+1, m_nTracks[i].IsAudio ? "audio" : "data ",
                                hms(m_nTracks[i].Length));
            }
            CONS_Printf ("\2Total time : %s\n", hms(CD_TotalTime()));
        }
        if (cdPlaying)
        {
            CONS_Printf ("%s track : %d\n", cdLooping ? "looping" : "playing",
                         cdPlayTrack);
        }
        return;
    }

    if (!strncmp(s,"play",4))
    {
        I_PlayCD (atoi (COM_Argv (2)), false);
        return;
    }

    if (!strncmp(s,"stop",4))
    {
        I_StopCD ();
        return;
    }

    if (!strncmp(s,"loop",4))
    {
        I_PlayCD (atoi (COM_Argv (2)), true);
        return;
    }

    if (!strncmp(s,"resume",4))
    {
        I_ResumeCD ();
        return;
    }

    CONS_Printf ("cd command '%s' unknown\n", s);
}


// ------------
// I_ShutdownCD
// Shutdown CD Audio subsystem, release whatever was allocated
// ------------
void I_ShutdownCD (void)
{
    MCIERROR    iErr;

    if (!cdaudio_started)
        return;

    CONS_Printf ("I_ShutdownCD()\n");

    I_StopCD();

    // closes MCI CD
    if ((iErr = mciSendCommand(m_MCIOpen.wDeviceID, MCI_CLOSE, 0, (DWORD)NULL)))
        MCIErrorMessageBox (iErr);
}


// --------
// I_InitCD
// Init CD Audio subsystem
// --------
void I_InitCD (void)
{
        MCI_SET_PARMS   mciSet;
    MCIERROR    iErr;
    int         i;
    
    // We don't have an open device yet
        m_MCIOpen.wDeviceID = 0;
        m_nTracksCount = 0;

    cdaudio_started = false;

    m_MCIOpen.lpstrDeviceType = (LPCTSTR)MCI_DEVTYPE_CD_AUDIO;
        if ((iErr = mciSendCommand((MCIDEVICEID)NULL, MCI_OPEN, MCI_OPEN_TYPE|MCI_OPEN_TYPE_ID, (DWORD)&m_MCIOpen)))
        {
                MCIErrorMessageBox (iErr);
        return;
    }

        // Set the time format to track/minute/second/frame (TMSF).
    mciSet.dwTimeFormat = MCI_FORMAT_TMSF;
    if ((iErr = mciSendCommand(m_MCIOpen.wDeviceID, MCI_SET, MCI_SET_TIME_FORMAT, (DWORD)&mciSet)))
    {
                MCIErrorMessageBox (iErr);
        mciSendCommand(m_MCIOpen.wDeviceID, MCI_CLOSE, 0, (DWORD)NULL);
        return;
    }  

    I_AddExitFunc (I_ShutdownCD);
    cdaudio_started = true;

    CONS_Printf ("I_InitCD: CD Audio started\n");

    // last saved in config.cfg
    i = cd_volume.value;
    //I_SetVolumeCD (0);   // initialize to 0 for some odd cd drivers
    I_SetVolumeCD (i);   // now set the last saved volume

    for (i = 0; i < MAX_CD_TRACKS; i++)
        cdRemap[i] = i;

    if (!CD_ReadTrackInfo())
    {
        CONS_Printf("\2I_InitCD: no CD in player.\n");
        cdEnabled = false;
        cdValid = false;
    }
    else
    {
        cdEnabled = true;
        cdValid = true;
    }
    
    COM_AddCommand ("cd", Command_Cd_f);
}



// loop/go to next track when track is finished (if cd_update var is true)
// update the volume when it has changed (from console/menu)
// TODO: check for cd change and restart music ?
//
void I_UpdateCD (void)
{
        
}


//
void I_PlayCD (int nTrack, boolean bLooping)
{
        MCI_PLAY_PARMS  mciPlay;
    MCIERROR        iErr;
        
    if (!cdaudio_started || !cdEnabled)
        return;

    //faB: try again if it didn't work (just free the user of typing 'cd reset' command)
    if (!cdValid)
        cdValid = CD_ReadTrackInfo();
    if (!cdValid)
        return;

    // tracks start at 0 in the code..
    nTrack--;
    if (nTrack < 0 || nTrack >= m_nTracksCount)
        nTrack = nTrack % m_nTracksCount;

    nTrack = cdRemap[nTrack];

    if (cdPlaying)
    {
        if (cdPlayTrack == nTrack)
            return;
        I_StopCD ();
    }
    
    cdPlayTrack = nTrack;

    if (!m_nTracks[nTrack].IsAudio)
    {
        CONS_Printf ("\2CD Play: not an audio track\n");
        return;
    }

    cdLooping = bLooping;

    //faB: stop MIDI music, MIDI music will restart if volume is upped later
    cv_musicvolume.value = 0;
    I_StopSong (0);

    //faB: I don't use the notify message, I'm trying to minimize the delay
    mciPlay.dwCallback = (DWORD)hWndMain;
        mciPlay.dwFrom = MCI_MAKE_TMSF(nTrack+1, 0, 0, 0);
        if ((iErr = mciSendCommand(m_MCIOpen.wDeviceID, MCI_PLAY, MCI_FROM|MCI_NOTIFY, (DWORD)&mciPlay)))
        {
                MCIErrorMessageBox (iErr);
        cdValid = false;
        cdPlaying = false;
        return;
        }

    cdPlaying = true;
}


// pause cd music
void I_StopCD (void)
{
    MCIERROR    iErr;

    if (!cdaudio_started || !cdEnabled)
        return;

    if ((iErr = mciSendCommand(m_MCIOpen.wDeviceID, MCI_PAUSE, MCI_WAIT, (DWORD)NULL)))
        MCIErrorMessageBox (iErr);
    else {
        wasPlaying = cdPlaying;
        cdPlaying = false;
    }
}


// continue after a pause
void I_ResumeCD (void)
{
    MCIERROR    iErr;

    if (!cdaudio_started || !cdEnabled)
        return;

    if (!cdValid)
        return;

    if (!wasPlaying)
        return;

    if ((iErr = mciSendCommand(m_MCIOpen.wDeviceID, MCI_RESUME, MCI_WAIT, (DWORD)NULL)))
                MCIErrorMessageBox (iErr);
    else
        cdPlaying = true;
}


// volume : logical cd audio volume 0-31 (hardware is 0-255)
int I_SetVolumeCD (int volume)
{
    return 1;
}