2016-01-19 8 views
0

Я реализовал прямой звуковой класс, основанный на tutorial, но столкнулся с проблемами при его тестировании. Я проследил ошибку в IDirectSound8 :: SetCooperativeLevel, который возвращается с ошибкой.IDirectSound8 SetCooperativeLevel Возврат Неверный параметр

Вот snipit моего метода DirectSound инициализации

HRESULT r; 
DSBUFFERDESC bufferDesc; 
WAVEFORMATEX wavFormat; 

r = DirectSoundCreate8(NULL, &DSound, NULL); 
if (FAILED(r)) 
{ 
    _com_error error(r); 
    LPCTSTR errText = error.ErrorMessage(); 
    OutputDebugString(errText); 
    return false; 
} 
OutputDebugStringA("\nCOMPLETE: DSound Stage 1 "); 

r = DSound->SetCooperativeLevel(hwnd, DSSCL_PRIORITY); 
if (FAILED(r)) 
{ 
    _com_error error(r); 
    LPCTSTR errText = error.ErrorMessage(); 
    OutputDebugString(errText); 
    return false; 
} 
OutputDebugStringA("\nCOMPLETE: DSound Stage 2 "); 

Программа получает мимо моего «DSound Stage 1», но не в r = DSound->SetCooperativeLevel(hwnd, DSSCL_PRIORITY); с указанием «неправильный параметр.»

На данный момент я не знаю, почему эта ошибка возникает или как ее исправить. Я предполагаю, что проблема связана с параметром HWND, который я передаю, но опять же я не знаю, виновато это или нет.

Возможно, эта проблема вызвана тем, что физическое окно не создается во время выполнения? Под этим я подразумеваю, что когда я компилирую этот проект, окна не создается - хотя кажется, что должно быть.

Здесь Main.cpp, где установлен HWND, который я отправляю в свой класс аудио, стоит отметить, что я не писал этот файл и не совсем понял, должно ли оно создавать окно или нет.

#include <windows.h> 
#include <windowsx.h> 

#include "RenderEngine.h" 
#include "Timer.h" 
#include "Audio.h" 

//WindowProc function prototype 
LRESULT CALLBACK WindowProc(HWND hWnd, 
         UINT message, 
         WPARAM wParam, 
         LPARAM lParam); 

int WINAPI WinMain(HINSTANCE hInstance,  //Handle to and Instance, This is how windows keeps track of which program is which. 
       HINSTANCE hPrevInstance, //Handle to the Previous Instance, this is a backwards compatibility requirement 
       LPSTR lpCmdLine,   //This is a Long Pointer to a string that contains the command line creating the application. 
       int nShowCmd)    //This determines what the window will look like. 
{ 
//First we create our Handle for the Window 
HWND hWnd; 
//Next we create our WindowClass Struct 
WNDCLASSEX wc; 
/////////////////////////////////////////////////////////////// 
//Create our Render Engine Class 
Timer GameTimer; 
RenderEngine Renderer; 
/////////////////////////////////////////////////////////////// 
//Create our Audio Class 
Audio* audio; 
/////////////////////////////////////////////////////////////// 

//Ensure the class is empty for use... 
ZeroMemory(&wc, sizeof(WNDCLASSEX)); 

//Initialize the Struct 
wc.cbSize = sizeof(WNDCLASSEX); 
wc.style = CS_HREDRAW | CS_VREDRAW; 
wc.lpfnWndProc = WindowProc; 
wc.hInstance = hInstance; 
wc.hCursor = LoadCursor(NULL, IDC_ARROW); 
wc.hbrBackground = (HBRUSH)COLOR_WINDOW; 
wc.lpszClassName = L"WIndowClass1"; 

//Regist the Window Class 
RegisterClassEx(&wc); 

//Create the windows and use the result as the handle 
hWnd = CreateWindowEx(NULL, 
         L"GSP420 WindowClass", //Name of the Window Class 
         L"GSP420 Project",  //Title of the Window 
         WS_OVERLAPPEDWINDOW,  //Window style 
         300,      //x-position of the Window 
         300,      //y-position of the Window 
         SCREENWIDTH,    //Width of the Window 
         SCREENHEIGHT,    //Heigt of the Window 
         NULL,      //There is no parent window 
         NULL,      //No menus are being used 
         hInstance,    //The application handle 
         NULL);     //We're not using Multiple-Windows 

//Display the Window 
ShowWindow(hWnd, nShowCmd); 

//////////////////////////////////////////////////////////////////// 
//Setup and Initialize Direct3D 
Renderer.initD3D(hWnd); 
//////////////////////////////////////////////////////////////////// 
//Setup and Initialize DirectSound 
OutputDebugStringA("BEGIN: AudioInit"); 
audio = new Audio; 
audio->AudioInit(hWnd); 
OutputDebugStringA("\nCOMPLETE: AudioInit\n"); 
//////////////////////////////////////////////////////////////////// 

//Enter the main loop 

//Windows Event Message Struct 
MSG msg; 

//Enter our Loop 
while (TRUE) 
{ 
    while (PeekMessage(&msg, NULL, 0, 0, PM_REMOVE)) 
    { 
     //Translate keystrokes into the correct format 
     TranslateMessage(&msg); 

     //Send the messages to WindowProc 
     DispatchMessage(&msg); 
    } 

    //If the message is WM_QUIT, exit the loop 
    if (msg.message == WM_QUIT) 
     break; 

    //////////////////////////////////// 
    ////////RUN OUR GAME CODE HERE////// 
    //////////////////////////////////// 
    GameTimer.calculateTime(); 

    Renderer.renderFrame(); 
    //////////////////////////////////// 
    ////////RUN OUR GAME CODE HERE////// 
    //////////////////////////////////// 
} 
//////////////////////////////////////////////////////// 
//Clean up DirectX and COM 
Renderer.cleanD3D(); 

//Return this part of the WM_QUIT message to Windows 
return msg.wParam; 
} 

//Main Message Handler for the Program 
LRESULT CALLBACK WindowProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam) 
{ 
//Sort through and find the code to execute 
switch (message) 
{ 
    //This message is read when the window is closed 
    case WM_DESTROY: 
    { 
     //Close the application 
     PostQuitMessage(0); 
     return 0; 
    }break; 
} 

//Handle any messages the switch statement didn't 
return DefWindowProc(hWnd, message, wParam, lParam); 
} 

И для справки всего метода InitDSound

bool Audio::InitDSound(HWND hwnd) 
{ 
HRESULT r;             // Create result variable 
DSBUFFERDESC bufferDesc; 
WAVEFORMATEX wavFormat; 

r = DirectSoundCreate8(NULL, &DSound, NULL);    // Initialize DSound 
if (FAILED(r))            // Check result, break if initialization failed 
{ 
    _com_error error(r); 
    LPCTSTR errText = error.ErrorMessage(); 
    OutputDebugString(errText); 
    return false; 
} 
OutputDebugStringA("\nCOMPLETE: DSound Stage 1 "); 

r = DSound->SetCooperativeLevel(hwnd, DSSCL_PRIORITY);  // No idea, allows the format of the primary buffer to be modified 
if (FAILED(r))            // Check result, break if that thing didnt work 
{ 
    _com_error error(r); 
    LPCTSTR errText = error.ErrorMessage(); 
    OutputDebugString(errText); 
    return false; 
} 
OutputDebugStringA("\nCOMPLETE: DSound Stage 2 "); 

////////////////////////////////// 
// Primary Buffer Descritpion 
////////////////////////////////// 
bufferDesc.dwSize = sizeof(DSBUFFERDESC); 
bufferDesc.dwFlags = DSBCAPS_PRIMARYBUFFER | DSBCAPS_CTRLVOLUME; 
bufferDesc.dwBufferBytes = 0; 
bufferDesc.dwReserved = 0; 
bufferDesc.lpwfxFormat = NULL; 
bufferDesc.guid3DAlgorithm = GUID_NULL; 

r = DSound->CreateSoundBuffer(&bufferDesc, &pBuffer, NULL); // Get control of the primary sound buffer on the sounde device 
if (FAILED(r))            // Check result, break if that failed 
{ 
    _com_error error(r); 
    LPCTSTR errText = error.ErrorMessage(); 
    OutputDebugString(errText); 
    return false; 
} 
OutputDebugStringA("\nCOMPLETE: DSound Stage 3 "); 

////////////////////////////////// 
// Primary Buffer Format 
// (WAV @44,100 16bit stereo) 
////////////////////////////////// 
wavFormat.wFormatTag = WAVE_FORMAT_PCM; 
wavFormat.nSamplesPerSec = 44100; 
wavFormat.wBitsPerSample = 16; 
wavFormat.nChannels = 2; 
wavFormat.nBlockAlign = (wavFormat.wBitsPerSample/8) * wavFormat.nChannels; 
wavFormat.nAvgBytesPerSec = wavFormat.nSamplesPerSec * wavFormat.nBlockAlign; 
wavFormat.cbSize = 0; 

r = pBuffer->SetFormat(&wavFormat);       // Set the primary buffer format 
if (FAILED(r))            // Check result, break if that failed 
{ 
    _com_error error(r); 
    LPCTSTR errText = error.ErrorMessage(); 
    OutputDebugString(errText); 
    return false; 
} 
OutputDebugStringA("\nCOMPLETE: DSound Stage 4 "); 

return true; 
} 

ответ

0

В Windows Vista или более поздняя версия, нет первичного буфера, так или иначе, и все это эмулируется через WASAPI. Драйвер определяет скорость вывода и всегда преобразуется в значения float. Вам следует просто использовать DSSCL_NORMAL, потому что это ничего не значит, и формат основного буфера не имеет значения.

Если вы не нацелены на Windows XP, вам следует избегать использования DirectSound, который не обновлялся через 16 лет. Посмотрите на XAudio2 или сторонние аудиомоторы, такие как FMOD, Wwise, MSS, OpenAL и т. Д., Все из которых реализуют свои собственные микшеры и используют WASAPI для окончательного вывода.

 Смежные вопросы

  • Нет связанных вопросов^_^