Я пытаюсь создать простой звуковой буфер и воспроизвести его с помощью XAudio 2.
Сначала я инициализировал IXAudio2
IXAudio2* XAudio = 0;
int XAudioInitFailed = XAudio2Create(&XAudio, 0, XAUDIO2_DEFAULT_PROCESSOR);
//check for failure
IXAudio2MasteringVoice* MasterVoice = 0;
XAudio->CreateMasteringVoice(&MasterVoice);
Затем я заполняю свои данные.
// Mono 8-bit 44.1kHz PCM format
WAVEFORMATEX WaveFormat = {};
WaveFormat.wFormatTag = WAVE_FORMAT_PCM;
WaveFormat.nChannels = 1;
WaveFormat.nSamplesPerSec = 44100;
WaveFormat.wBitsPerSample = 8;
WaveFormat.nBlockAlign = WaveFormat.nChannels * WaveFormat.wBitsPerSample / 8;
WaveFormat.nAvgBytesPerSec = WaveFormat.nSamplesPerSec * WaveFormat.nBlockAlign;
WaveFormat.cbSize = sizeof(WAVEFORMATEX);
// Populating my SoundBuffer
// Creating XAudio sound buffer struct
IXAudio2SourceVoice* SourceVoice = 0;
int code = XAudio->CreateSourceVoice(&SourceVoice, &WaveFormat);
SourceVoice->SubmitSourceBuffer(&XBuffer); //this line triggers exceptions
SourceVoice->Start(0);
И когда я запускаю код, исключение срабатывает в строке SourceVoice->SubmitSourceBuffer(&XBuffer);
, код равен 0x88960001
, что означает XAUDIO2_E_INVALID_CALL
в соответствии со страницей API Microsoft Windows.
Помогите выяснить, в чем проблема.
ОБНОВЛЕНИЕ : Полный код:
WinAudio = {};
IXAudio2* XAudio = 0;
int XAudioInitFailed = XAudio2Create(&XAudio, 0, XAUDIO2_DEFAULT_PROCESSOR);
if(!XAudioInitFailed) {
//NOTE: creating the mastering voice
IXAudio2MasteringVoice* MasterVoice = 0;
XAudio->CreateMasteringVoice(&MasterVoice);
//NOTE: initializing PlayBuffer
// Mono 8-bit 44.1kHz PCM format
WAVEFORMATEX WaveFormat = {};
WaveFormat.wFormatTag = WAVE_FORMAT_PCM;
WaveFormat.nChannels = 1;
WaveFormat.nSamplesPerSec = 44100;
WaveFormat.wBitsPerSample = 8;
WaveFormat.nBlockAlign = WaveFormat.nChannels * WaveFormat.wBitsPerSample / 8;
WaveFormat.nAvgBytesPerSec = WaveFormat.nSamplesPerSec * WaveFormat.nBlockAlign;
WaveFormat.cbSize = sizeof(WAVEFORMATEX);
// NOTE: initializing SoundBuffer
int SampleFrequency = 256;
int Samples = WaveFormat.nSamplesPerSec;
int AudioBytes = Samples * WaveFormat.nBlockAlign;
i1* SoundBuffer = (i1*)malloc(AudioBytes);
i1* WritePointer = SoundBuffer;
for(int Sample = 0;
Sample < Samples;
Sample ++) {
i1 value = ((Sample / SampleFrequency)%2)*10;
*WritePointer++ = value;
//*WritePointer++ = value;
}
// NOTE: Creating Sound
XAUDIO2_BUFFER XBuffer = {};
XBuffer.Flags = XAUDIO2_END_OF_STREAM;
XBuffer.AudioBytes = AudioBytes;
XBuffer.pAudioData = (const BYTE*)SoundBuffer;
// NOTE: The following parameters are
// measured in samples
XBuffer.PlayBegin = 0;
XBuffer.PlayLength = 0;
XBuffer.LoopBegin = 0;
XBuffer.LoopLength = 0;
XBuffer.LoopCount = XAUDIO2_LOOP_INFINITE;
IXAudio2SourceVoice* SourceVoice = 0;
int code = XAudio->CreateSourceVoice(&SourceVoice, &WaveFormat);
SourceVoice->SubmitSourceBuffer(&XBuffer);
SourceVoice->Start(0);
} else {
// TODO: Diagnose
InvalidCodePath;
}