• wav转txt格式的代码实现(c,python)


      平时经常做音频算法,经常用得到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, "
    ");
    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
    	");
    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

  • 相关阅读:
    卸载cuda,以及N卡驱动
    ubuntu 16.04 从gcc 5.4 安装gcc 5.3.0
    Check failed: status == CUBLAS_STATUS_SUCCESS (13 vs. 0) CUBLAS_STATUS_EXECUTION_FAILED
    ubuntu16.04 caffe cuda9.1 segnet nvidia gpu安装注意的点
    ubuntu16.04安装docker
    进程管理
    Dev TextEdit 只输入数字
    dev gridcontrol添加右键菜单
    WinForm rdlc 报表自定义datatable数据源
    DevExpress GridControl使用方法总结2 属性说明
  • 原文地址:https://www.cnblogs.com/dylancao/p/9776530.html
Copyright © 2020-2023  润新知