magicstix,
The reason you are getting a clicking is because you are not correctly converting your float samples to bytes in your streamNextChunk function. X86-based OS like Windows use "little-endian" bit ordering for byte arrays. Your 32-bit(4-byte) samples are arranged MSB-->LSB, where the order goes highest byte to lowest byte called "big endian"(like a written number). You will need to correctly convert each sample into 4-bytes to populate the sound buffer which is "little-endian": LSB-->MSB. The clicking is a result of each sample having its 4-bytes in the reverse order(DCBA), and the audio engine expecting the correct order: ABCD. Here is some code that should fix for you:
// Write each 32-bit sample as 4 bytes(order reversed) into your XAUDIO2_BUFFER (buf)
for(int i=0; i < buf.AudioBytes/4; i++) {
// Convert each sample to 4 bytes
byte byte4 = (byte) (samples[i] >> 24);
byte byte3 = (byte) (samples[i] >> 16);
byte byte2 = (byte) (samples[i] >> 8);
byte byte1 = (byte) (samples[i] >> 0);
// Insert each each sample into buffer in reverse byte-order
buf.audioData[4*i] = byte4;
buf.audioData[4*i+1] = byte3;
buf.audioData[4*i+2] = byte2;
buf.audioData[4*i+3] = byte1;
}
Hope this helps!
Adam
Show differencesHistory of post edits
#Actualadamw86
Posted 04 January 2013 - 06:20 PM
#5adamw86
Posted 04 January 2013 - 06:16 PM
magicstix,
The reason you are getting a clicking is because you are not correctly converting your float samples to bytes in your streamNextChunk function. X86-based OS like Windows use "little-endian" bit ordering for byte arrays. Your 32-bit(4-byte) samples are arranged MSB-->LSB, where the order goes highest byte to lowest byte called "big endian"(like a written number). You will need to correctly convert each sample into 4-bytes to populate the sound buffer which is "little-endian": LSB-->MSB. The clicking is a result of each sample having its 4-bytes in the reverse order(4321), and the audio engine expecting the correct order: 1234. Here is some code that should fix for you:
// Write each 32-bit sample as 4 bytes(order reversed) into your XAUDIO2_BUFFER (buf)
for(int i=0; i < buf.AudioBytes/4; i++) {
// Convert each sample to 4 bytes
byte byte4 = (byte) (samples[i] >> 24);
byte byte3 = (byte) (samples[i] >> 16);
byte byte2 = (byte) (samples[i] >> 8);
byte byte1 = (byte) (samples[i] >> 0);
// Insert each each sample into buffer in reverse byte-order
buf.audioData[4*i] = byte4;
buf.audioData[4*i+1] = byte3;
buf.audioData[4*i+2] = byte2;
buf.audioData[4*i+3] = byte1;
}
Hope this helps!
Adam
#4adamw86
Posted 04 January 2013 - 06:07 PM
magicstix,
The reason you are getting a clicking is because you are not correctly converting your float samples to bytes in your streamNextChunk function. X86-based OS like Windows use "little-endian" bit ordering for byte arrays. Your 32-bit(4-byte) samples are arranged MSB-->LSB, where the order goes highest byte to lowest byte called "big endian"(like a written number). You will need to correctly convert each sample into 4-bytes to populate the sound buffer which is "little-endian": LSB-->MSB. The clicking is a result of each sample having its 4-bytes in the reverse order(DCBA), and the audio engine expecting the correct order: ABCD. Here is some code that should fix for you:
// Write each 32-bit sample as 4 bytes(order reversed) into your XAUDIO2_BUFFER (buf)
for(int i=0; i < buf.AudioBytes/4; i++) {
// Convert each sample to 4 bytes
byte byte4 = (byte) (samples[i] >> 24);
byte byte3 = (byte) (samples[i] >> 16);
byte byte2 = (byte) (samples[i] >> 8);
byte byte1 = (byte) (samples[i] >> 0);
// Insert each DCBA-ordered sample into buffer in ABCD-order
buf.audioData[4*i] = byte4;
buf.audioData[4*i+1] = byte3;
buf.audioData[4*i+2] = byte2
buf.audioData[4*i+3] = byte1;
}
Hope this helps!
Adam
#3adamw86
Posted 04 January 2013 - 06:07 PM
The reason you are getting a clicking is because you are not correctly converting your float samples to bytes in your streamNextChunk function. X86-based OS like Windows use "little-endian" bit ordering for byte arrays. Your 32-bit(4-byte) samples are arranged MSB-->LSB, where the order goes highest byte to lowest byte called "big endian"(like a written number). You will need to correctly convert each sample into 4-bytes to populate the sound buffer which is "little-endian": LSB-->MSB. The clicking is a result of each sample having its 4-bytes in the reverse order(DCBA), and the audio engine expecting the correct order: ABCD. Here is some code that should fix for you:
// Write each 32-bit sample as 4 bytes(order reversed) into your XAUDIO2_BUFFER (buf)
for(int i=0; i < buf.AudioBytes/4; i++) {
// Convert each sample to 4 bytes
byte byte4 = (byte) (samples[i] >> 24);
byte byte3 = (byte) (samples[i] >> 16);
byte byte2 = (byte) (samples[i] >> 8);
byte byte1 = (byte) (samples[i] >> 0);
// Insert each DCBA-ordered sample into buffer as 4 bytes: ABCD
buf.audioData[4*i] = byte4;
buf.audioData[4*i+1] = byte3;
buf.audioData[4*i+2] = byte2
buf.audioData[4*i+3] = byte1;
}
Hope this helps!
Adam
#2adamw86
Posted 04 January 2013 - 06:06 PM
The reason you are getting a clicking is because you are not correctly converting your float samples to bytes in your streamNextChunk function. X86-based OS like Windows use "little-endian" bit ordering for byte arrays. Your 32-bit(4-byte) samples are arranged MSB-->LSB, where the order goes highest byte to lowest byte called "big endian"(like a written number). You will need to correctly convert each sample into 4-bytes to populate the sound buffer which is "little-endian": LSB-->MSB. The clicking is a result of each sample having its 4-bytes in the reverse order(DCBA), and the audio engine expecting the correct order: ABCD. Here is some code that should fix for you:
// Write each 32-bit sample as 4 bytes(order reversed) into your XAUDIO2_BUFFER (buf)
for(int i=0; i < buf.AudioBytes/4; i++) {
// Convert each sample to 4 bytes
byte byte4 = (byte) (samples[i] >> 24);
byte byte3 = (byte) (samples[i] >> 16);
byte byte2 = (byte) (samples[i] >> 8);
byte byte1 = (byte) (samples[i] >> 0);
// Insert each DCBA-ordered sample into buffer as 4 bytes: ABCD
buf.audioData[4*i] = byte4;
buf.audioData[4*i+1] = byte3;
buf.audioData[4*i+2] = byte2
buf.audioData[4*i+3] = byte1;
}
Hope this helps!
Adam
#1adamw86
Posted 04 January 2013 - 05:54 PM
magicstix,
The reason you are getting a clicking is likely because you are not correctly converting your float samples to bytes in your streamNextChunk function. X86-based OS like Windows use "little-endian" bit ordering for byte arrays. Your 32-bit(4-byte) samples are arranged MSB-->LSB, where the order goes highest byte to lowest byte called "big endian"(like a written number). You will need to correctly convert each sample into 4-bytes to populate the sound buffer which is "little-endian": LSB-->MSB. The clicking is a result of each sample having its 4-bytes in the reverse order(DCBA), and the audio engine expecting the correct order: ABCD. Here is some code that should fix for you: