XAudio2 error: AudioBytes not aligned to the audio format's block size

Category: windows metro apps games

Question

Soft Sell Studios on Wed, 23 Oct 2013 00:40:41


After playing a sound effect an error is thrown at this code:

DX::ThrowIfFailed(
        m_sourceVoice->SubmitSourceBuffer(&buffer)
        );

Under the output window I found this.

XAudio2 error: AudioBytes not aligned to the audio format's block size
XAudio2 error: Invalid source buffer

Any help would be appreciated, thanks.


[url=http://catalog.create.msdn.com/en-US/GameDetails.aspx?catalogEntryId=5cbacba6-05c2-4c33-9005-6cc80c8d5753&type=1]Bible Trivia Avatar Edition[/url], currently in review.



Replies

James Dailey - MSFT on Wed, 23 Oct 2013 01:07:12


Hello,

How big is your buffer in bytes? What is the audio format that you are using? If your audio format is 16 bits and you have two stereo channels then your block align would be 32 bits or 4 bytes. This means that the size of your buffer must be a multiple of 4 bytes.

I hope this helps,

James 

Soft Sell Studios on Wed, 23 Oct 2013 13:12:28


The audio format is 16 bits and stereo channels, yes.

How can I ensure that the size of my buffer is a multiple of 4 bytes?

ZeroMemory(&buffer, sizeof(XAUDIO2_BUFFER));
    buffer.AudioBytes = m_soundData->Length;
    buffer.pAudioData = m_soundData->Data;
    buffer.Flags = XAUDIO2_END_OF_STREAM;

This is the code to set up the audio buffer. But it seems, if I adjust the size of the .AudioBytes, then the sounds come out choppy.

Sorry if I didn't clear this up, but the actual error is:

XAudio2 error: AduioBytes not aligned to the audio format's block size
XAudio2 error: Invalid source buffer

nigelwright7557 on Wed, 23 Oct 2013 21:07:53


The code I have sets up a waveformat:

		
  // Create an IXAudio2 object
  HRESULT hr = XAudio2Create(&pXAudio2);
  if (FAILED(hr))
  {
//    errorText->Text = "XAudio2Create failure: " + hr.ToString();
    return;
  }
  // Create a mastering voice
  hr = pXAudio2->CreateMasteringVoice(&pMasteringVoice);
  if (FAILED(hr))
  {
//    errorText->Text = "CreateMasteringVoice failure: " + hr.ToString();
    return;
  }
  // Create a source voice
  WAVEFORMATEX waveformat;
  waveformat.wFormatTag = WAVE_FORMAT_PCM;
  waveformat.nChannels = 1;
  waveformat.nSamplesPerSec = 44100;
  waveformat.nAvgBytesPerSec = 44100 * 2;
  waveformat.nBlockAlign = 2;
  waveformat.wBitsPerSample = 16;
  waveformat.cbSize = 0;

 
	  hr = pXAudio2->CreateSourceVoice(&pSourceVoice, &waveformat);
	if (FAILED(hr))
	{
	//   errorText->Text = "CreateSourceVoice failure: " + hr.ToString();
    return;
	}

  // Start the source voice
  hr = pSourceVoice->Start();
  if (FAILED(hr))
  {
 //   errorText->Text = "Start failure: " + hr.ToString();
    return;
  }




	
	 XAUDIO2_BUFFER buffer = { 0 };
  buffer.AudioBytes = 2 * 5 * 44100;
  buffer.pAudioData = soundData;
  buffer.Flags = XAUDIO2_END_OF_STREAM;
  buffer.PlayBegin = 0;
  buffer.PlayLength = soundlength/3;
  // Submit the buffer
 hr = pSourceVoice->SubmitSourceBuffer(&buffer);
  if (FAILED(hr))
  {
//    errorText->Text = "SubmitSourceBuffer failure: " + hr.ToString();
//    submitButton->Visibility = Windows::UI::Xaml::Visibility::Collapsed;
    return;
  }

Chuck Walbourn - MSFT on Wed, 23 Oct 2013 21:30:57


AudioBytes must be evenly divisible by nBlockAlign.

The full error message includes the values. They are likely important to understand this error.

"AudioBytes (%lu) not aligned to the audio format's block size (%lu)"