Aaron Elkins
2018-09-30 13:20:55 UTC
Hi, all
I am working on a project which get PCM data streaming over Bluetooth, and I encounter some glitch (random noise) while playing PCM data via callback mode, here is a snippets of my code:
```
Below is the callback:
```
static int patestCallback( const void *inputBuffer, void *outputBuffer,
unsigned long framesPerBuffer,
const PaStreamCallbackTimeInfo* timeInfo,
PaStreamCallbackFlags statusFlags,
void *userData )
{
paTestData *data = (paTestData*)userData;
short *out = (short*)outputBuffer;
unsigned long i, j;
(void) timeInfo; /* Prevent unused variable warnings. */
(void) statusFlags;
(void) inputBuffer;
uint32_t availableBytes = 0;
unsigned char *b = TPCircularBufferTail(&data->buffer, &availableBytes);
//NSLog(@"[in] avbytes: %d framesPerBuffer: %lu", availableBytes, framesPerBuffer);
uint32_t frames = availableBytes / 2 / 2;
if (availableBytes <= 0) return paContinue;
uint32_t availableFrames = (uint32_t) MIN(frames, framesPerBuffer);
uint32_t index = 0;
for(i = 0; i < availableFrames; i++){
for( j = 0; j < CHANNEL_COUNT; ++j ){
unsigned char high = b[index];
unsigned char low = b[index+1];
short s = low + (high << 8);
//s = s * 5;
*out++ = s;
index += 2;
}
}
TPCircularBufferConsume(&data->buffer, (uint32_t)index);
return paContinue;
}
```
And below is where I add PCM data segment into TPCircularBuffer` (In Objective-C)
Here is where I get PCM data from other Bluetooth device. If I write a segment of PCM data into file, and then load this file into a large buffer, and then add it to the TPCircularBuffer, I did
not hear the glitch, the glitch only occurs while in live PCM streaming.
```
- (void)a2dpSink:(A2DPSink *)sink channel:(IOBluetoothL2CAPChannel *)channel rawRTPdataReceived:(NSData *)data
{
NSData *mediaPayload = getMediaPayloadInRTP(data);
getSBCFramesInMediaPayload(mediaPayload);
#define BUF_SIZE 1024*1024*5
unsigned char *buf = (unsigned char*)malloc(BUF_SIZE);
memset(buf, 0, BUF_SIZE);
long index = 0;
uint16 i;
for (i = 0; i < [sbcFrames count]; i++) {
long size = [[sbcFrames objectAtIndex:i] length];
memcpy((unsigned char*)buf + index, (unsigned char*)[[sbcFrames objectAtIndex:i] bytes], size);
index += size;
}
[sbcFrames removeAllObjects];
// Start decode
int pcmBytes = 0;
unsigned char *pcm = malloc(1024*1024*5);
decodeSBCFramesBuffer(buf, (int)index, pcm, &pcmBytes);
//printf("Count: %d\n", pcmBytes);
TPCircularBufferProduceBytes(&paData.buffer, pcm, pcmBytes);
if (!Pa_IsStreamActive(stream)) {
e = Pa_StartStream(stream);
}
}
Sorry for the poor format of this email, I want to know why the glitch noise only happens in the live PCM streaming mode, what’s wrong with my code?
Thank you.
-Aaron
I am working on a project which get PCM data streaming over Bluetooth, and I encounter some glitch (random noise) while playing PCM data via callback mode, here is a snippets of my code:
```
Below is the callback:
```
static int patestCallback( const void *inputBuffer, void *outputBuffer,
unsigned long framesPerBuffer,
const PaStreamCallbackTimeInfo* timeInfo,
PaStreamCallbackFlags statusFlags,
void *userData )
{
paTestData *data = (paTestData*)userData;
short *out = (short*)outputBuffer;
unsigned long i, j;
(void) timeInfo; /* Prevent unused variable warnings. */
(void) statusFlags;
(void) inputBuffer;
uint32_t availableBytes = 0;
unsigned char *b = TPCircularBufferTail(&data->buffer, &availableBytes);
//NSLog(@"[in] avbytes: %d framesPerBuffer: %lu", availableBytes, framesPerBuffer);
uint32_t frames = availableBytes / 2 / 2;
if (availableBytes <= 0) return paContinue;
uint32_t availableFrames = (uint32_t) MIN(frames, framesPerBuffer);
uint32_t index = 0;
for(i = 0; i < availableFrames; i++){
for( j = 0; j < CHANNEL_COUNT; ++j ){
unsigned char high = b[index];
unsigned char low = b[index+1];
short s = low + (high << 8);
//s = s * 5;
*out++ = s;
index += 2;
}
}
TPCircularBufferConsume(&data->buffer, (uint32_t)index);
return paContinue;
}
```
And below is where I add PCM data segment into TPCircularBuffer` (In Objective-C)
Here is where I get PCM data from other Bluetooth device. If I write a segment of PCM data into file, and then load this file into a large buffer, and then add it to the TPCircularBuffer, I did
not hear the glitch, the glitch only occurs while in live PCM streaming.
```
- (void)a2dpSink:(A2DPSink *)sink channel:(IOBluetoothL2CAPChannel *)channel rawRTPdataReceived:(NSData *)data
{
NSData *mediaPayload = getMediaPayloadInRTP(data);
getSBCFramesInMediaPayload(mediaPayload);
#define BUF_SIZE 1024*1024*5
unsigned char *buf = (unsigned char*)malloc(BUF_SIZE);
memset(buf, 0, BUF_SIZE);
long index = 0;
uint16 i;
for (i = 0; i < [sbcFrames count]; i++) {
long size = [[sbcFrames objectAtIndex:i] length];
memcpy((unsigned char*)buf + index, (unsigned char*)[[sbcFrames objectAtIndex:i] bytes], size);
index += size;
}
[sbcFrames removeAllObjects];
// Start decode
int pcmBytes = 0;
unsigned char *pcm = malloc(1024*1024*5);
decodeSBCFramesBuffer(buf, (int)index, pcm, &pcmBytes);
//printf("Count: %d\n", pcmBytes);
TPCircularBufferProduceBytes(&paData.buffer, pcm, pcmBytes);
if (!Pa_IsStreamActive(stream)) {
e = Pa_StartStream(stream);
}
}
Sorry for the poor format of this email, I want to know why the glitch noise only happens in the live PCM streaming mode, what’s wrong with my code?
Thank you.
-Aaron