Example #1
0
XAudPlayer::XAudPlayer()
{
   m_stream = NULL;

#ifdef DEBUG_NO_SOUND
   return;
#endif

   if (!bass_init)
   {
      int DSidx;
      const HRESULT hr = GetRegInt("Player", "SoundDeviceBG", &DSidx);
      if (hr != S_OK)
          DSidx = -1;

      // now match the Direct Sound device with the BASS device (by name)
      int BASSidx = -1;
      if (DSidx != -1)
      {
          DSAudioDevices DSads;
          if (!FAILED(DirectSoundEnumerate(DSEnumCallBack, &DSads)))
          {
              if ((unsigned int)DSidx >= DSads.size() || DSads[DSidx]->guid != NULL) // primary device has guid NULL, so use BASSidx = -1 in that case
              {
                  BASS_DEVICEINFO info;
                  for (int i = 1; BASS_GetDeviceInfo(i, &info); i++) // 0 = no sound/no device
                      if (info.flags & BASS_DEVICE_ENABLED) // device must be enabled
                      if (strcmp(info.name, DSads[DSidx]->description.c_str()) == 0)
                      {
                          BASSidx = i;
                          break;
                      }
              }

              for (size_t i = 0; i < DSads.size(); i++)
                  delete DSads[i];
          }
      }

      if (!BASS_Init(BASSidx, 44100, 0, g_pvp->m_hwnd, NULL)) // note that sample rate is usually ignored and set depending on the input/file automatically
      {
         char bla[128];
         sprintf_s(bla, "BASS music/sound library initialization error %d", BASS_ErrorGetCode());
         MessageBox(g_pvp->m_hwnd, bla, "Error", MB_ICONERROR);
      }
      bass_init = true;
   }
}
Example #2
0
void PinDirectSound::InitDirectSound(HWND hwnd, bool IsBackglass)
{
#ifdef DEBUG_NO_SOUND
    return;
#endif

    HRESULT hr;
    LPDIRECTSOUNDBUFFER pDSBPrimary = NULL;

    // Initialize COM
    //if( hr = CoInitialize( NULL ) )
    //return hr;

    DSAudioDevices DSads;
    int DSidx = 0;
    if (!FAILED(DirectSoundEnumerate(DSEnumCallBack, &DSads)))
    {
        hr = GetRegInt("Player", IsBackglass ? "SoundDeviceBG" : "SoundDevice", &DSidx);
        if ((hr != S_OK) || ((unsigned int)DSidx >= DSads.size()))
            DSidx = 0; // The default primary sound device
    }

    // Create IDirectSound using the selected sound device
    if (FAILED(hr = DirectSoundCreate((DSidx != 0) ? DSads[DSidx]->guid : NULL, &m_pDS, NULL)))
    {
        ShowError("Could not create Direct Sound.");
        return;// hr;
    }

    // free audio devices list
    for (size_t i = 0; i < DSads.size(); i++)
        delete DSads[i];

    // Set coop level to DSSCL_PRIORITY
    if (FAILED(hr = m_pDS->SetCooperativeLevel(hwnd, DSSCL_PRIORITY)))
    {
        ShowError("Could not set Direct Sound Priority.");
        return;// hr;
    }

    // Get the primary buffer 
    DSBUFFERDESC dsbd;
    ZeroMemory(&dsbd, sizeof(DSBUFFERDESC));
    dsbd.dwSize = sizeof(DSBUFFERDESC);
    dsbd.dwFlags = DSBCAPS_PRIMARYBUFFER;
    dsbd.dwBufferBytes = 0;
    dsbd.lpwfxFormat = NULL;

    if (FAILED(hr = m_pDS->CreateSoundBuffer(&dsbd, &pDSBPrimary, NULL)))
    {
        ShowError("Could not create primary sound buffer.");
        return;// hr;
    }

    // Set primary buffer format to 44kHz and 16-bit output.
    WAVEFORMATEX wfx;
    ZeroMemory(&wfx, sizeof(WAVEFORMATEX));
    wfx.wFormatTag = WAVE_FORMAT_PCM;
    wfx.nChannels = 2;
    wfx.nSamplesPerSec = 44100;
    wfx.wBitsPerSample = 16;
    wfx.nBlockAlign = wfx.wBitsPerSample / (WORD)8 * wfx.nChannels;
    wfx.nAvgBytesPerSec = wfx.nSamplesPerSec * wfx.nBlockAlign;

    if (FAILED(hr = pDSBPrimary->SetFormat(&wfx)))
    {
        ShowError("Could not set sound format.");
        return;// hr;
    }

    SAFE_RELEASE(pDSBPrimary);

    //return S_OK;
}