Fork me on GitHub

随笔 - 997  文章 - 5  评论 - 181  阅读 - 300万 

  平时经常做音频算法,经常用得到wav转txt的转换,这里就做个备忘,自己写了一些小代码来实现这个目标:

第一个是c代码的实现:

复制代码
 1 #include <stdio.h>
 2 #include <stdlib.h>
 3 #include <math.h>
 4 
 5 #define W 128
 6 
 7 int FileSet = 0;
 8 int FileEnd = 0;
 9 int FileLength = 0;
10 short InputData[W];
11 void buf_txt(short *buf,int len,FILE*ttt)
12 {
13     for(int i=0; i < len; i++)
14     {   
15         fprintf(ttt, ",%d",buf[i]);
16         if(i % 64 == 0)
17         {   
18             fprintf(ttt, "\n");
19         }   
20     }   
21 }
22 
23 int main(int argc, char** argv)
24 {
25 
26     FILE *Ifp,*ttt;
27         
28     if(argc != 2)
29     {   
30         printf("usage:./wav2txt XX.wav EEE.txt\n\t");
31         return 0;
32     }   
33 
34     Ifp = fopen(argv[1],"rb");
35 
36     ttt = fopen(argv[2],"w");
37 
38     fseek(Ifp,0L,SEEK_END);
39     FileEnd=ftell(Ifp);
40     rewind(Ifp);
41     FileLength = FileEnd/2;
42 
43     while(FileLength >= W)
44     {   
45         fread(InputData,sizeof(short),W,Ifp);
46         buf_txt(InputData,W,ttt);
47         FileLength -= W;
48         
49     }   
50 
51     fread(InputData,sizeof(short),FileLength,Ifp);
52 
53     buf_txt(InputData,FileLength,ttt);
54 
55     return 0;
56 }
57 ~    
复制代码

  

第二个是python的代码:

 

复制代码
 1 # -*- coding: utf-8 -*-
 2 import wave
 3 import matplotlib.pyplot as plt 
 4 import numpy as np
 5 import sys
 6 
 7 
 8 f = wave.open(sys.argv[1], 'rb' )
 9 params = f.getparams()
10 nchannels, sampwidth, framerate, nframes = params[:4]
11 
12 np.set_printoptions(threshold='nan')
13 
14 Data_str = f.readframes(nframes)
15 Data_num = np.fromstring(Data_str,dtype=np.int16)
16 print(Data_num)
17 print(nframes)
18 
19 #np.savetxt("test.txt",Data_num)
20 
21 fw =file(sys.argv[2],"w")
22 fw.write(str(list(Data_num)))
23 fw.close()
24 
25 f.close()
复制代码

 

python和c代码放在一起的时候,才会发现,它是多么的简洁,看来以后要经常使用了。把python作为一个重点使用的语言来重视起来。

 

 备忘问题:

1 一个数组无穷大,numpy在输出时会自动省略中间部分而只打印部分。

  解决方法:使用numpy.set_printoptions(threshold='nan')

2 Python中list里面的元素没有以逗号为分割,而是以空格为分割:

 解决方法:基础知识不够牢固,list中元素是以逗号做分割的,matrix中是以空格为分割的,强转即可。

参考文档:

1 https://www.cnblogs.com/dupuleng/articles/5028291.html

2 https://blog.csdn.net/bowean/article/details/80868965

posted on   虚生  阅读(3133)  评论(0编辑  收藏  举报
编辑推荐:
· 浏览器原生「磁吸」效果!Anchor Positioning 锚点定位神器解析
· 没有源码,如何修改代码逻辑?
· 一个奇形怪状的面试题:Bean中的CHM要不要加volatile?
· [.NET]调用本地 Deepseek 模型
· 一个费力不讨好的项目,让我损失了近一半的绩效!
阅读排行:
· 微软正式发布.NET 10 Preview 1:开启下一代开发框架新篇章
· 没有源码,如何修改代码逻辑?
· PowerShell开发游戏 · 打蜜蜂
· 在鹅厂做java开发是什么体验
· WPF到Web的无缝过渡:英雄联盟客户端的OpenSilver迁移实战
历史上的今天:
2017-10-12 搭建git远程服务器三步骤
2017-10-12 详解m4文件
2016-10-12 ROM、RAM、DRAM、SRAM和FLASH的区别
点击右上角即可分享
微信分享提示