Ignore:
Timestamp:
Feb 27, 2000, 9:29:47 PM (25 years ago)
Author:
sandervl
Message:

Implemented waveOutGetPosition

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/winmm/dwaveout.cpp

    r2812 r2935  
    1 /* $Id: dwaveout.cpp,v 1.14 2000-02-17 14:09:30 sandervl Exp $ */
     1/* $Id: dwaveout.cpp,v 1.15 2000-02-27 20:29:46 sandervl Exp $ */
    22
    33/*
     
    3131#include "dwaveout.h"
    3232
    33 #define DBG_LOCALLOG    DBG_dwaveout
     33#define DBG_LOCALLOG    DBG_dwaveout
    3434#include "dbglocal.h"
    3535
     
    6565
    6666   if(!ulError)
    67         callback((ULONG)this, WOM_OPEN, dwInstance, 0, 0);
     67    callback((ULONG)this, WOM_OPEN, dwInstance, 0, 0);
    6868}
    6969/******************************************************************************/
     
    7676
    7777   if(!ulError)
    78         PostMessageA(hwndCallback, WOM_OPEN, 0, 0);
     78    PostMessageA(hwndCallback, WOM_OPEN, 0, 0);
    7979}
    8080/******************************************************************************/
     
    157157   DeviceId = AmpOpenParms.usDeviceID;
    158158   if(rc) {
    159 #ifdef DEBUG
    160     WriteLog("MCI_OPEN failed\n");
    161 #endif
     159    dprintf(("MCI_OPEN failed\n"));
    162160    mciError(rc);
    163161    ulError = MMSYSERR_NODRIVER;
     
    169167                        (PVOID)&GenericParms, 0);
    170168    if(rc) {
    171 #ifdef DEBUG
    172         WriteLog("MCI_ACQUIREDEVICE failed\n");
    173 #endif
     169        dprintf(("MCI_ACQUIREDEVICE failed\n"));
    174170        mciError(rc);
    175171        ulError = MMSYSERR_NOTENABLED;
     
    209205
    210206   if(!ulError) {
    211         // Generic parameters
    212         GenericParms.hwndCallback = 0;   //hwndFrame
    213 
    214         // Stop the playback.
    215         mciSendCommand(DeviceId, MCI_STOP,MCI_WAIT, (PVOID)&GenericParms,0);
    216 
    217         mciSendCommand(DeviceId,
     207    // Generic parameters
     208    GenericParms.hwndCallback = 0;   //hwndFrame
     209
     210    // Stop the playback.
     211    mciSendCommand(DeviceId, MCI_STOP,MCI_WAIT, (PVOID)&GenericParms,0);
     212
     213    mciSendCommand(DeviceId,
    218214                  MCI_BUFFER,
    219215                  MCI_WAIT | MCI_DEALLOCATE_MEMORY,
     
    221217                  0);
    222218
    223         // Generic parameters
    224         GenericParms.hwndCallback = 0;   //hwndFrame
    225 
    226         // Close the device
    227         mciSendCommand(DeviceId, MCI_CLOSE, MCI_WAIT, (PVOID)&GenericParms, 0);
     219    // Generic parameters
     220    GenericParms.hwndCallback = 0;   //hwndFrame
     221
     222    // Close the device
     223    mciSendCommand(DeviceId, MCI_CLOSE, MCI_WAIT, (PVOID)&GenericParms, 0);
    228224   }
    229225
    230226   if(wmutex)
    231         wmutex->enter(VMUTEX_WAIT_FOREVER);
     227        wmutex->enter(VMUTEX_WAIT_FOREVER);
    232228
    233229   State = STATE_STOPPED;
    234230
    235231   if(waveout == this) {
    236     waveout = this->next;
     232        waveout = this->next;
    237233   }
    238234   else {
    239     DartWaveOut *dwave = waveout;
    240 
    241     while(dwave->next != this) {
    242         dwave = dwave->next;
    243     }
    244     dwave->next = this->next;
     235        DartWaveOut *dwave = waveout;
     236
     237        while(dwave->next != this) {
     238            dwave = dwave->next;
     239        }
     240        dwave->next = this->next;
    245241   }
    246242   if(wmutex)
    247     wmutex->leave();
     243        wmutex->leave();
    248244
    249245   if(!ulError) {
    250         if(mthdCallback) {
    251                 callback((ULONG)this, WOM_CLOSE, dwInstance, 0, 0);
    252         }
    253         else
    254         if(hwndCallback)
    255                 PostMessageA(hwndCallback, WOM_CLOSE, 0, 0);
     246        if(mthdCallback) {
     247            callback((ULONG)this, WOM_CLOSE, dwInstance, 0, 0);
     248        }
     249        else
     250        if(hwndCallback)
     251            PostMessageA(hwndCallback, WOM_CLOSE, 0, 0);
    256252   }
    257253
    258254   if(wmutex)
    259     delete wmutex;
     255        delete wmutex;
    260256
    261257   if(MixBuffer)
    262     free(MixBuffer);
     258        free(MixBuffer);
    263259   if(MixSetupParms)
    264     free(MixSetupParms);
     260        free(MixSetupParms);
    265261   if(BufferParms)
    266     free(BufferParms);
     262        free(BufferParms);
    267263}
    268264/******************************************************************************/
     
    292288
    293289   if(rc) {
    294         return 0; //no devices present
     290        return 0; //no devices present
    295291   }
    296292
     
    311307 int i, buflength;
    312308
    313   if(fMixerSetup == FALSE) {
    314 #ifdef DEBUG
    315     WriteLog("device acquired\n");
    316 #endif
    317     /* Set the MixSetupParms data structure to match the loaded file.
    318      * This is a global that is used to setup the mixer.
    319      */
    320     memset(MixSetupParms, 0, sizeof( MCI_MIXSETUP_PARMS ) );
    321 
    322     MixSetupParms->ulBitsPerSample = BitsPerSample;
    323     MixSetupParms->ulSamplesPerSec = SampleRate;
    324     MixSetupParms->ulFormatTag     = MCI_WAVE_FORMAT_PCM;
    325     MixSetupParms->ulChannels      = nChannels;
    326 
    327 #ifdef DEBUG
    328     WriteLog("bps %d, sps %d chan %d\n", BitsPerSample, SampleRate, nChannels);
    329 #endif
     309  if(fMixerSetup == FALSE)
     310  {
     311        dprintf(("device acquired\n"));
     312        /* Set the MixSetupParms data structure to match the loaded file.
     313         * This is a global that is used to setup the mixer.
     314         */
     315        memset(MixSetupParms, 0, sizeof( MCI_MIXSETUP_PARMS ) );
     316
     317        MixSetupParms->ulBitsPerSample = BitsPerSample;
     318        MixSetupParms->ulSamplesPerSec = SampleRate;
     319        MixSetupParms->ulFormatTag     = MCI_WAVE_FORMAT_PCM;
     320        MixSetupParms->ulChannels      = nChannels;
     321
     322        dprintf(("bps %d, sps %d chan %d\n", BitsPerSample, SampleRate, nChannels));
    330323
    331324        /* Setup the mixer for playback of wave data
    332325         */
    333     MixSetupParms->ulFormatMode = MCI_PLAY;
     326        MixSetupParms->ulFormatMode = MCI_PLAY;
    334327        MixSetupParms->ulDeviceType = MCI_DEVTYPE_WAVEFORM_AUDIO;
    335328        MixSetupParms->pmixEvent    = WaveOutHandler;
     
    341334                            0);
    342335
    343     if ( rc != MCIERR_SUCCESS ) {
    344         mciError(rc);
    345         mciSendCommand(DeviceId, MCI_RELEASEDEVICE, MCI_WAIT,
    346                    (PVOID)&GenericParms, 0);
    347         return(MMSYSERR_NOTSUPPORTED);
    348     }
     336        if ( rc != MCIERR_SUCCESS ) {
     337            mciError(rc);
     338            mciSendCommand(DeviceId, MCI_RELEASEDEVICE, MCI_WAIT,
     339                           (PVOID)&GenericParms, 0);
     340            return(MMSYSERR_NOTSUPPORTED);
     341        }
    349342
    350343        /*
     
    352345         * device buffers from the Amp-Mixer
    353346         */
    354 #ifdef DEBUG
    355     WriteLog("mix setup %d, %d\n", pwh->dwBufferLength, pwh->dwBufferLength);
    356 #endif
     347        dprintf(("mix setup %d, %d\n", pwh->dwBufferLength, pwh->dwBufferLength));
     348
    357349#if 1
    358     ulBufSize = pwh->dwBufferLength/2;
     350        ulBufSize = pwh->dwBufferLength/2;
    359351#else
    360     if(pwh->dwBufferLength >= 512 && pwh->dwBufferLength <= 1024)
    361         ulBufSize = pwh->dwBufferLength;
    362     else    ulBufSize = 1024;
    363 #endif
    364 
    365     MixSetupParms->ulBufferSize = ulBufSize;
     352        if(pwh->dwBufferLength >= 512 && pwh->dwBufferLength <= 1024)
     353                ulBufSize = pwh->dwBufferLength;
     354        else    ulBufSize = 1024;
     355#endif
     356
     357        MixSetupParms->ulBufferSize = ulBufSize;
    366358
    367359        BufferParms->ulNumBuffers = PREFILLBUF_DART;
     
    370362
    371363        for(i=0;i<PREFILLBUF_DART;i++) {
    372         MixBuffer[i].ulUserParm = (ULONG)this;
     364            MixBuffer[i].ulUserParm = (ULONG)this;
    373365        }
    374366
     
    380372
    381373        if(ULONG_LOWD(rc) != MCIERR_SUCCESS) {
    382         mciError(rc);
    383         mciSendCommand(DeviceId, MCI_RELEASEDEVICE, MCI_WAIT,
    384                    (PVOID)&GenericParms, 0);
    385         return(MMSYSERR_NOTSUPPORTED);
    386     }
    387 
    388     wmutex->enter(VMUTEX_WAIT_FOREVER);
    389     fMixerSetup = TRUE;
    390 
    391     curPlayBuf = curFillBuf = curFillPos = curPlayPos = 0;
     374            mciError(rc);
     375            mciSendCommand(DeviceId, MCI_RELEASEDEVICE, MCI_WAIT,
     376                          (PVOID)&GenericParms, 0);
     377            return(MMSYSERR_NOTSUPPORTED);
     378        }
     379
     380        wmutex->enter(VMUTEX_WAIT_FOREVER);
     381        fMixerSetup = TRUE;
     382
     383        curPlayBuf = curFillBuf = curFillPos = curPlayPos = 0;
    392384
    393385        for(i=0;i<PREFILLBUF_DART;i++) {
    394         memset(MixBuffer[i].pBuffer, 0, MixBuffer[i].ulBufferLength);
    395     }
    396 #ifdef DEBUG
    397     WriteLog("Dart opened, bufsize = %d\n", MixBuffer[i].ulBufferLength);
    398 #endif
    399 
    400     wavehdr     = pwh;
    401     curhdr      = pwh;
    402     pwh->lpNext = NULL;
    403 
    404     while(TRUE) {
    405         buflength = min((ULONG)MixBuffer[curFillBuf].ulBufferLength - curPlayPos,
    406                 (ULONG)wavehdr->dwBufferLength - curFillPos);
    407 #ifdef DEBUG
    408         WriteLog("Copying %d data; curPlayPos = %d curFillPos = %d\n", buflength, curPlayPos, curFillPos);
    409 #endif
    410         memcpy((char *)MixBuffer[curFillBuf].pBuffer + curPlayPos,
    411                wavehdr->lpData + curFillPos,
    412                buflength);
    413 
    414         curPlayPos  += buflength;
    415         curFillPos += buflength;
    416         if(curFillPos == wavehdr->dwBufferLength) {
    417 #ifdef DEBUG
    418             WriteLog("Processed first win32 buffer\n");
    419 #endif
    420             curFillPos        = 0;
    421             wavehdr->dwFlags |= WHDR_DONE;
    422             curhdr            = NULL;
    423         }
    424         if(curPlayPos == MixBuffer[curPlayBuf].ulBufferLength) {
    425             if(++curPlayBuf == PREFILLBUF_DART) {
    426                 curPlayBuf = 0;
     386            memset(MixBuffer[i].pBuffer, 0, MixBuffer[i].ulBufferLength);
     387        }
     388        dprintf(("Dart opened, bufsize = %d\n", MixBuffer[i].ulBufferLength));
     389
     390        wavehdr     = pwh;
     391        curhdr      = pwh;
     392        pwh->lpNext = NULL;
     393
     394        while(TRUE) {
     395            buflength = min((ULONG)MixBuffer[curFillBuf].ulBufferLength - curPlayPos,
     396                            (ULONG)wavehdr->dwBufferLength - curFillPos);
     397            dprintf(("Copying %d data; curPlayPos = %d curFillPos = %d\n", buflength, curPlayPos, curFillPos));
     398
     399            memcpy((char *)MixBuffer[curFillBuf].pBuffer + curPlayPos,
     400                   wavehdr->lpData + curFillPos,
     401                   buflength);
     402
     403            curPlayPos  += buflength;
     404            curFillPos += buflength;
     405            if(curFillPos == wavehdr->dwBufferLength) {
     406                dprintf(("Processed first win32 buffer\n"));
     407                curFillPos        = 0;
     408                wavehdr->dwFlags |= WHDR_DONE;
     409                curhdr            = NULL;
     410            }
     411            if(curPlayPos == MixBuffer[curPlayBuf].ulBufferLength) {
     412                if(++curPlayBuf == PREFILLBUF_DART) {
     413                    curPlayBuf = 0;
     414                    break;
     415                }
     416                curPlayPos = 0;
     417            }
     418            if(curFillPos == 0)
    427419                break;
     420        }
     421        dprintf(("MixSetupParms = %X\n", MixSetupParms));
     422        State = STATE_PLAYING;
     423        wmutex->leave();
     424
     425        MixSetupParms->pmixWrite(MixSetupParms->ulMixHandle,
     426                                 MixBuffer,
     427                                 PREFILLBUF_DART);
     428        dprintf(("Dart playing\n"));
     429  }
     430  else
     431  {
     432        wmutex->enter(VMUTEX_WAIT_FOREVER);
     433        pwh->lpNext   = NULL;
     434        if(wavehdr) {
     435            WAVEHDR *chdr = wavehdr;
     436            while(chdr->lpNext) {
     437                chdr = chdr->lpNext;
    428438            }
    429             curPlayPos = 0;
    430         }
    431         if(curFillPos == 0)
    432             break;
    433     }
    434 #ifdef DEBUG
    435     WriteLog("MixSetupParms = %X\n", MixSetupParms);
    436 #endif
    437     State = STATE_PLAYING;
    438     wmutex->leave();
    439 
    440     MixSetupParms->pmixWrite(MixSetupParms->ulMixHandle,
    441                              MixBuffer,
    442                              PREFILLBUF_DART);
    443 #ifdef DEBUG
    444     WriteLog("Dart playing\n");
    445 #endif
    446   }
    447   else {
    448     wmutex->enter(VMUTEX_WAIT_FOREVER);
    449     pwh->lpNext   = NULL;
    450     if(wavehdr) {
    451         WAVEHDR *chdr = wavehdr;
    452         while(chdr->lpNext) {
    453             chdr = chdr->lpNext;
    454         }
    455         chdr->lpNext = pwh;
    456     }
    457     else    wavehdr = pwh;
    458     wmutex->leave();
    459     if(State != STATE_PLAYING) {//continue playback
    460         restart();
    461     }
     439            chdr->lpNext = pwh;
     440        }
     441        else    wavehdr = pwh;
     442        wmutex->leave();
     443        if(State != STATE_PLAYING) {//continue playback
     444            restart();
     445        }
    462446  }
    463447
     
    471455
    472456  if(State != STATE_PLAYING)
    473     return(MMSYSERR_HANDLEBUSY);
     457        return(MMSYSERR_NOERROR);
    474458
    475459  wmutex->enter(VMUTEX_WAIT_FOREVER);
     
    492476  dprintf(("DartWaveOut::reset %s", (State == STATE_PLAYING) ? "playing" : "stopped"));
    493477  if(State != STATE_PLAYING)
    494     return(MMSYSERR_HANDLEBUSY);
     478        return(MMSYSERR_HANDLEBUSY);
    495479
    496480  memset(&Params, 0, sizeof(Params));
     
    499483  mciSendCommand(DeviceId, MCI_STOP, MCI_WAIT, (PVOID)&Params, 0);
    500484
    501 #ifdef DEBUG
    502   WriteLog("Nr of threads blocked on mutex = %d\n", wmutex->getNrBlocked());
    503 #endif
     485  dprintf(("Nr of threads blocked on mutex = %d\n", wmutex->getNrBlocked()));
    504486
    505487  wmutex->enter(VMUTEX_WAIT_FOREVER);
     
    510492        callback((ULONG)this, WOM_DONE, dwInstance, wavehdr->dwUser, (ULONG)wavehdr);
    511493    }
    512     else
     494    else {
    513495        if(hwndCallback)
    514         PostMessageA(hwndCallback, WOM_DONE, wavehdr->dwUser, (ULONG)wavehdr);
    515 
     496            PostMessageA(hwndCallback, WOM_DONE, wavehdr->dwUser, (ULONG)wavehdr);
     497    }
    516498    wmutex->enter(VMUTEX_WAIT_FOREVER);
    517499    wavehdr = wavehdr->lpNext;
     
    527509MMRESULT DartWaveOut::restart()
    528510{
    529   dprintf(("DartWaveOut::restart"));
    530   wmutex->enter(VMUTEX_WAIT_FOREVER);
    531   State = STATE_PLAYING;
    532   wmutex->leave();
    533   MixSetupParms->pmixWrite(MixSetupParms->ulMixHandle,
    534                           &MixBuffer[curPlayBuf],
    535                           PREFILLBUF_DART);
    536   return(MMSYSERR_NOERROR);
     511    dprintf(("DartWaveOut::restart"));
     512    wmutex->enter(VMUTEX_WAIT_FOREVER);
     513    State = STATE_PLAYING;
     514    wmutex->leave();
     515    MixSetupParms->pmixWrite(MixSetupParms->ulMixHandle,
     516                             &MixBuffer[curPlayBuf],
     517                             PREFILLBUF_DART);
     518    return(MMSYSERR_NOERROR);
     519}
     520/******************************************************************************/
     521/******************************************************************************/
     522ULONG DartWaveOut::getPosition()
     523{
     524 MCI_STATUS_PARMS mciStatus = {0};
     525 ULONG rc, nrbytes;
     526
     527    if(State != STATE_PLAYING)
     528        return 0;
     529
     530    mciStatus.ulItem = MCI_STATUS_POSITION;
     531    rc = mciSendCommand(DeviceId, MCI_STATUS, MCI_STATUS_ITEM|MCI_WAIT, (PVOID)&mciStatus, 0);
     532    if((rc & 0xFFFF) == MCIERR_SUCCESS) {
     533        nrbytes = (mciStatus.ulReturn * getAvgBytesPerSecond())/1000;
     534        return nrbytes;;
     535    }
     536    mciError(rc);
     537    return 0xFFFFFFFF;
    537538}
    538539/******************************************************************************/
     
    560561                       0);
    561562  if (rc != 0) {
    562     return(FALSE);
     563        return(FALSE);
    563564  }
    564565  DeviceId = mciOpenParms.usDeviceID;
     
    591592                      0);
    592593  if((rc & 0xFFFF) != MCIERR_SUCCESS) {
    593         mciError(rc);
    594         winrc = FALSE;
    595   }
    596   else  winrc = TRUE;
     594        mciError(rc);
     595        winrc = FALSE;
     596  }
     597  else  winrc = TRUE;
    597598
    598599  // Close the device
     
    608609
    609610  mciGetErrorString(ulError, szError, sizeof(szError));
    610   WriteLog("WINMM: DartWaveOut: %s\n", szError);
     611  dprintf(("WINMM: DartWaveOut: %s\n", szError));
    611612#endif
    612613}
     
    637638
    638639#ifdef DEBUG1
    639   WriteLog("WINMM: handler %d\n", curPlayBuf);
     640  dprintf(("WINMM: handler %d\n", curPlayBuf));
    640641#endif
    641642  if(ulFlags == MIX_STREAM_ERROR) {
     
    653654    if(whdr->dwFlags & WHDR_DONE) {
    654655#ifdef DEBUG1
    655         WriteLog("WINMM: handler buf %X done\n", whdr);
     656        dprintf(("WINMM: handler buf %X done\n", whdr));
    656657#endif
    657658        whdr->dwFlags &= ~WHDR_INQUEUE;
     
    681682
    682683#ifdef DEBUG1
    683   WriteLog("WINMM: handler cur (%d,%d), fill (%d,%d)\n", curPlayBuf, curPlayPos, curFillBuf, curFillPos);
     684  dprintf(("WINMM: handler cur (%d,%d), fill (%d,%d)\n", curPlayBuf, curPlayPos, curFillBuf, curFillPos));
    684685#endif
    685686
     
    693694    curFillPos += buflength;
    694695#ifdef DEBUG1
    695     WriteLog("WINMM: copied %d bytes, cufFillPos = %d, dwBufferLength = %d\n", buflength, curFillPos, curhdr->dwBufferLength);
     696    dprintf(("WINMM: copied %d bytes, cufFillPos = %d, dwBufferLength = %d\n", buflength, curFillPos, curhdr->dwBufferLength));
    696697#endif
    697698    if(curFillPos == curhdr->dwBufferLength) {
    698699#ifdef DEBUG1
    699         WriteLog("Buffer %d done\n", curFillBuf);
     700        dprintf(("Buffer %d done\n", curFillBuf));
    700701#endif
    701702        curFillPos = 0;
     
    747748/******************************************************************************/
    748749MMRESULT DartWaveOut::setVolume(ULONG ulVol)
    749 { 
    750   ULONG ulVolR     = (((ulVol & 0xffff0000) >> 16 )*100)/0xFFFF; // Right Volume 
     750{
     751  ULONG ulVolR     = (((ulVol & 0xffff0000) >> 16 )*100)/0xFFFF; // Right Volume
    751752  ULONG ulVolL      = ((ulVol& 0x0000ffff)*100)/0xFFFF;          // Left Volume
    752753  MCI_SET_PARMS msp = {0};
     
    757758//     one channel Left or Right :-(
    758759//
    759 #ifdef GOOD_AUDIO_CARD_DRIVER 
     760#ifdef GOOD_AUDIO_CARD_DRIVER
    760761
    761762  msp.ulAudio = MCI_SET_AUDIO_LEFT;
Note: See TracChangeset for help on using the changeset viewer.