Chinaunix首页 | 论坛 | 博客
  • 博客访问: 521826
  • 博文数量: 86
  • 博客积分: 1076
  • 博客等级: 准尉
  • 技术积分: 1018
  • 用 户 组: 普通用户
  • 注册时间: 2011-11-02 19:15
文章分类

全部博文(86)

文章存档

2013年(15)

2012年(69)

2011年(2)

分类: LINUX

2012-05-24 12:27:14

结合上篇提供的ADPCM的编解码代码,给出测试用例:
 
1、编码testc.c

  1. /* testc - Test adpcm coder */
  2.    
  3. #include "adpcm.h"
  4. #include <stdio.h>
  5. #include <stdlib.h>
  6. #include <string.h>
  7.    
  8. static struct adpcm_state state;
  9.    
  10. #define NSAMPLES 1600
  11.    
  12. char abuf[NSAMPLES/2];
  13. short sbuf[NSAMPLES];
  14.    
  15. void main(int argc, char *argv[])
  16. {
  17.     FILE *f, *f2;
  18.     char fname[64],fname2[64];
  19.    
  20.     if (argc < 3 )
  21.     {
  22.         printf("Usage: %s infile outfile\r\n", argv[0]);
  23.         exit(1);
  24.     }
  25.     strcpy(fname, argv[1]);
  26.     strcpy(fname2, argv[2]);
  27.    
  28.     if (NULL == (f2=fopen(fname2,"wb")) )
  29.     {
  30.         printf("Error opening '%s', terminating..\r\n", fname2);
  31.         exit(4);
  32.     }
  33.     if (NULL == (f=fopen(fname,"rb")) )
  34.     {
  35.         printf("Error opening '%s', terminating..\r\n", fname);
  36.         exit(4);
  37.     }
  38.     while(fread((void *)sbuf, sizeof(sbuf), 1, f) )
  39.     {
  40.         adpcm_coder(sbuf, abuf, sizeof(sbuf), &state);
  41.         fwrite((void *)abuf, sizeof(abuf), 1, f2);
  42.     }
  43.     fclose(f);
  44.     fclose(f2);
  45.     printf("Final valprev=%d, index=%d\r\n",
  46.            state.valprev, state.index);
  47.     exit(0);
  48. }
 
2、解码testd.c

 
  1. /* testd - Test adpcm decoder */
  2.    
  3. #include "adpcm.h"
  4. #include <stdio.h>
  5. #include <stdlib.h>
  6. #include <string.h>
  7.    
  8. static struct adpcm_state state;
  9.    
  10. #define NSAMPLES 1600
  11.    
  12. char abuf[NSAMPLES/2];
  13. short sbuf[NSAMPLES];
  14.    
  15. void main(int argc, char *argv[])
  16. {
  17.     FILE *f, *f2;
  18.     char fname[64],fname2[64];
  19.    
  20.     if (argc < 3 )
  21.     {
  22.         printf("Usage: %s infile outfile\r\n", argv[0]);
  23.         exit(1);
  24.     }
  25.     strcpy(fname, argv[1]);
  26.     strcpy(fname2, argv[2]);
  27.    
  28.     if ( NULL == (f2=fopen(fname2,"wb")) )
  29.     {
  30.         printf("Error opening '%s', terminating..\r\n", fname2);
  31.         exit(4);
  32.     }
  33.     if ( NULL == (f=fopen(fname,"rb")) )
  34.     {
  35.         printf("Error opening '%s', terminating..\r\n", fname);
  36.         exit(4);
  37.     }
  38.     
  39.     while( fread((void *)abuf, 1, sizeof(abuf), f) )
  40.     {
  41.         adpcm_decoder(abuf, sbuf, sizeof(abuf), &state);
  42.         fwrite((void *)sbuf, 1, sizeof(sbuf), f2);
  43.     }
  44.     fclose(f);
  45.     fclose(f2);
  46.     printf("Final valprev=%d, index=%d\r\n",
  47.            state.valprev, state.index);
  48.     exit(0);
  49. }
 
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

  1. #! /bin/sh
  2. gcc testc.c adpcm.c -o testc
  3. gcc testd.c adpcm.c -o testd
  4. gcc data_to_wav.c -o data_to_wav

  5. ./testc source.bin source-encode.bin
  6. ./testd source-encode.bin source-decode.bin
  7. ./data_to_wav source-decode.bin source-decode.wav
 
6、所有代码的打包
 代码打包.rar   
另外推荐两款音频处理软件,音频处理的有:GoldWave和专业级的Adobe Audition。
转码类的软件有:格式工厂和MediaCoder。

阅读(5220) | 评论(3) | 转发(1) |
给主人留下些什么吧!~~

JGFNTU2014-01-20 19:08:04

什么都可以11111:博主你好,我现在也在做ADPCM,有个问题想玩一下你。
define NSAMPLES 1600 ,这个1600的值是怎么计算来的?

你好,这个时间有点久了,应该是8KHz,16bit,单声道吧

回复 | 举报

什么都可以111112013-12-24 16:45:39

博主你好,我现在也在做ADPCM,有个问题想玩一下你。
define NSAMPLES 1600 ,这个1600的值是怎么计算来的?