I have been working on getting the volume control to work. The example code only gets and sets the local variable volume but does not actually change the output volume when played on a speaker. Does anybody know how to successfully change the volume?? Because the I2S is using PCM in this example, I attempted to change the output volume by bit shifting the data depending on the volume percentage (which is already a feature in the a2dp_sink example). The I2s transfers 2 bytes for each channel, so I thought that bit shifting two bytes of data to the right would decrease the volume, but it just makes it really noisy.
Here is code I edited in the a2dp_sink example:
Code: Select all
uint8_t masks[9] = {0x00,0x01,0x03,0x07,0x0f,0x1f,0x3f,0x7f,0xff};
size_t write_ringbuf(const uint8_t *data, size_t size)
{
const int numAmpBytes = 2;
uint8_t *writableData = (uint8_t*) data;
uint8_t mask;
uint8_t tempData;
for (size_t h = 0; h < size; h += numAmpBytes) {
mask = 0x00;
for (size_t i = h; i < (h + numAmpBytes); ++i) {
tempData = writableData[i];
writableData[i] = writableData[i] >> (8 - master_volume);
writableData[i] |= mask << (master_volume);
mask = tempData & masks[8 - master_volume];
}
}
BaseType_t done = xRingbufferSend(s_ringbuf_i2s, (void*) writableData , size, (portTickType)portMAX_DELAY);
if(done){
return size;
} else {
return 0;
}
}
Thanks everyone!