结合上篇提供的ADPCM的编解码代码,给出测试用例:
1、编码testc.c
- /* testc - Test adpcm coder */
-
- #include "adpcm.h"
- #include <stdio.h>
- #include <stdlib.h>
- #include <string.h>
-
- static struct adpcm_state state;
-
- #define NSAMPLES 1600
-
- char abuf[NSAMPLES/2];
- short sbuf[NSAMPLES];
-
- void main(int argc, char *argv[])
- {
- FILE *f, *f2;
- char fname[64],fname2[64];
-
- if (argc < 3 )
- {
- printf("Usage: %s infile outfile\r\n", argv[0]);
- exit(1);
- }
- strcpy(fname, argv[1]);
- strcpy(fname2, argv[2]);
-
- if (NULL == (f2=fopen(fname2,"wb")) )
- {
- printf("Error opening '%s', terminating..\r\n", fname2);
- exit(4);
- }
- if (NULL == (f=fopen(fname,"rb")) )
- {
- printf("Error opening '%s', terminating..\r\n", fname);
- exit(4);
- }
- while(fread((void *)sbuf, sizeof(sbuf), 1, f) )
- {
- adpcm_coder(sbuf, abuf, sizeof(sbuf), &state);
- fwrite((void *)abuf, sizeof(abuf), 1, f2);
- }
- fclose(f);
- fclose(f2);
- printf("Final valprev=%d, index=%d\r\n",
- state.valprev, state.index);
- exit(0);
- }
2、解码testd.c
- /* testd - Test adpcm decoder */
-
- #include "adpcm.h"
- #include <stdio.h>
- #include <stdlib.h>
- #include <string.h>
-
- static struct adpcm_state state;
-
- #define NSAMPLES 1600
-
- char abuf[NSAMPLES/2];
- short sbuf[NSAMPLES];
-
- void main(int argc, char *argv[])
- {
- FILE *f, *f2;
- char fname[64],fname2[64];
-
- if (argc < 3 )
- {
- printf("Usage: %s infile outfile\r\n", argv[0]);
- exit(1);
- }
- strcpy(fname, argv[1]);
- strcpy(fname2, argv[2]);
-
- if ( NULL == (f2=fopen(fname2,"wb")) )
- {
- printf("Error opening '%s', terminating..\r\n", fname2);
- exit(4);
- }
- if ( NULL == (f=fopen(fname,"rb")) )
- {
- printf("Error opening '%s', terminating..\r\n", fname);
- exit(4);
- }
-
- while( fread((void *)abuf, 1, sizeof(abuf), f) )
- {
- adpcm_decoder(abuf, sbuf, sizeof(abuf), &state);
- fwrite((void *)sbuf, 1, sizeof(sbuf), f2);
- }
- fclose(f);
- fclose(f2);
- printf("Final valprev=%d, index=%d\r\n",
- state.valprev, state.index);
- exit(0);
- }
3、音频用例
source.rar (不包含wav头部44字节,可使用下面提供的data_to_wav代码将44字节补全,以便跟采用编解码之后生成的音乐作对比)
这是一首《星光游乐园》歌曲,使用格式工厂转码为8KHz,16bit,单声道的wav(pcm)数据,使用ultraEdit剥离了44个字节的wav头文件,解压之后名为source.bin。使用testc可以将其压缩为source-encode.bin(这里你可以看到压缩比例在1:4),再使用testd将其解压缩为source-decode.bin(这个文件与源文件source.bin会有少许的偏差),然后使用data_to_wav将其添加44个字节wav格式的头文件,即可还原出原来的音乐。
4、提供添加44字节的wav头文件的代码data_to_wav.c
(详见附件代码)
5、给出编译运行的脚本my_adpcm.sh
- #! /bin/sh
- gcc testc.c adpcm.c -o testc
- gcc testd.c adpcm.c -o testd
- gcc data_to_wav.c -o data_to_wav
- ./testc source.bin source-encode.bin
- ./testd source-encode.bin source-decode.bin
- ./data_to_wav source-decode.bin source-decode.wav
6、所有代码的打包
代码打包.rar
另外推荐两款音频处理软件,音频处理的有:GoldWave和专业级的Adobe Audition。
转码类的软件有:格式工厂和MediaCoder。
阅读(3056) | 评论(1) | 转发(0) |