我是靠谱客的博主 幸福芒果,这篇文章主要介绍wav转amr以及简单调整音量,现在分享给大家,希望可以做个参考。

说一下wav转amr的方式。wav是PC上录制音频最容易生成的方式,但是缺点是生成的音频体积比较大。amr是手机上音频播放比较主流的格式,优点是音频体积小,易于传输。

转换的方式很简单,amr分两种,这里以nb为例。首先需要下载opencore-amr,将静态库和文件导入工程里。然后输入以下代码

复制代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
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 %sn", infile); return 1; } if (!wav_get_header(wav, &format, &channels, &sampleRate, &bitsPerSample, NULL)) { fprintf(stderr, "Bad wav file %sn", infile); return 1; } if (format != 1) { fprintf(stderr, "Unsupported WAV format %dn", format); return 1; } if (bitsPerSample != 16) { fprintf(stderr, "Unsupported WAV sample depth %dn", bitsPerSample); return 1; } if (channels != 1) fprintf(stderr, "Warning, only compressing one audio channeln"); 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("#!AMRn", 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,麦克风音量,合成器音量等等统统都不管用,起码我测试着效果不明显。既然录制时无法处理,只能在录制结束后对音频文件进行处理。这一篇博客有详细的说明,测试发现能正常运行,注意这里处理的音频,不包含音频头部:点击打开链接

测试发现音频如果将音量设置为大小一致是发不出声音的,所以具体调整需要另行设计算法,但是原理基本就是连接所说的。

最后

以上就是幸福芒果最近收集整理的关于wav转amr以及简单调整音量的全部内容,更多相关wav转amr以及简单调整音量内容请搜索靠谱客的其他文章。

本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
点赞(65)

评论列表共有 0 条评论

立即
投稿
返回
顶部