说一下wav转amr的方式。wav是PC上录制音频最容易生成的方式,但是缺点是生成的音频体积比较大。amr是手机上音频播放比较主流的格式,优点是音频体积小,易于传输。
转换的方式很简单,amr分两种,这里以nb为例。首先需要下载opencore-amr,将静态库和文件导入工程里。然后输入以下代码
int wav2Amr( const char *infile, const char *outfile) { enum Mode mode = MR122; int ch, dtx = 0; FILE *out; void *wav, *amr; int format, sampleRate, channels, bitsPerSample; int inputSize; uint8_t* inputBuf; int modeRate = 4750; mode = findMode(modeRate); wav = wav_read_open(infile); if (!wav) { fprintf(stderr, "Unable to open wav file %s\n", infile); return 1; } if (!wav_get_header(wav, &format, &channels, &sampleRate, &bitsPerSample, NULL)) { fprintf(stderr, "Bad wav file %s\n", infile); return 1; } if (format != 1) { fprintf(stderr, "Unsupported WAV format %d\n", format); return 1; } if (bitsPerSample != 16) { fprintf(stderr, "Unsupported WAV sample depth %d\n", bitsPerSample); return 1; } if (channels != 1) fprintf(stderr, "Warning, only compressing one audio channel\n"); if (sampleRate != 8000) fprintf(stderr, "Warning, AMR-NB uses 8000 Hz sample rate (WAV file has %d Hz)\n", sampleRate); inputSize = channels*2*160; inputBuf = (uint8_t*) malloc(inputSize); amr = Encoder_Interface_init(dtx); out = fopen(outfile, "wb"); if (!out) { perror(outfile); return 1; } fwrite("#!AMR\n", 1, 6, out); while (1) { short buf[160]; uint8_t outbuf[500]; int read, i, n; read = wav_read_data(wav, inputBuf, inputSize); read /= channels; read /= 2; if (read < 160) break; for (i = 0; i < 160; i++) { const uint8_t* in = &inputBuf[2*channels*i]; buf[i] = in[0] | (in[1] << 8); } n = Encoder_Interface_Encode(amr, mode, buf, outbuf, 0); fwrite(outbuf, 1, n, out); } free(inputBuf); fclose(out); Encoder_Interface_exit(amr); wav_read_close(wav); return 0; }代码是opencore的测试用例上的。可以直接拿来使用。
接下来说说调整音量的相关处理。测试发现什么设置主音量,wave,麦克风音量,合成器音量等等统统都不管用,起码我测试着效果不明显。既然录制时无法处理,只能在录制结束后对音频文件进行处理。这一篇博客有详细的说明,测试发现能正常运行,注意这里处理的音频,不包含音频头部:点击打开链接
测试发现音频如果将音量设置为大小一致是发不出声音的,所以具体调整需要另行设计算法,但是原理基本就是连接所说的。
原文地址:http://blog.csdn.net/helius_sun/article/details/44004493