///转换测试代码--------------------------------------------------------
- (NSData*) pcm_downsample:(AVAudioPCMBuffer*)buffer {
//位深转换 float32 转 int16_t
NSData* pData = [[NSData alloc] initWithBytes:buffer.floatChannelData[0] length:buffer.frameLength * 4];
Byte*dataByte = (Byte*)[pDatabytes];
NSUIntegersrcSize = pData.length/4;
Float32vf32[srcSize];
memcpy(vf32, &dataByte[0], pData.length);
int16_tbArr[srcSize];
intr;
for(size_ti =0; i < srcSize; ++i) {
floatx = vf32[i];
floatc;
c = ((x < -1) ? -1: ((x >1) ?1: x));
c = c +1;
r = (int)(c *32767.5f);
r = r -32768;
bArr[i] = (short)r;
}
// NSMutableData *data2 = [[NSMutableData alloc] init];
// for (NSUInteger i = 0; i < srcSize; i++) {
//
// unsigned char valChar[2];
// valChar[0] = 0xff & bArr[i];
// valChar[1] = (0xff00 & bArr[i]) >> 8;
// [data2 appendBytes:valChar length:2];
// }
// return data2;
//码率转换 44100 转 16000
uint32_tlast_pos = (uint32_t)(srcSize -1);
uint32_tdstSize = (srcSize/44100.0)*16000;
int16_tbArr16[dstSize];
for(uint32_tidx =0; idx < dstSize; idx++) {
floatindex = ((float) idx *44100.0) / (16000.0);
uint32_tp1 = (uint32_t) index;
floatcoef = index - p1;
uint32_tp2 = (p1 == last_pos) ? last_pos : p1 +1;
bArr16[idx] = (int16_t) ((1.0f- coef) * bArr[p1] + coef * bArr[p2]);
//NSLog(@"%d",bArr16[idx]);
}
NSMutableData *data1 = [[NSMutableData alloc] init];
for(NSUIntegeri =0; i < dstSize; i++) {
unsignedcharvalChar[2];
valChar[0] =0xff& bArr16[i];
valChar[1] = (0xff00& bArr16[i]) >>8;
[data1appendBytes:valCharlength:2];
}
returndata1;
}
网友评论