IDirectSound8 SetCooperativeLevel, возвращающий неверный параметр

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

Вот фрагмент моего метода инициализации 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 для окончательного вывода.

0

Другие решения

Других решений пока нет …

По вопросам рекламы ammmcru@yandex.ru
Adblock
detector