因在NBM3108/XVF3000中加入开机提示音, 需用把wav文件转成数组, 步骤如下:
1. 用audacity 打开wav文件. 设置采样率为8KHz, 单声道, 然后导出音频, 得到8K/16bit的wav 文件;
2. 用下面的wav2txt.py 生成PCMdata8K16bit.h 文件. 文件中定义了一个 int16_t tonePCM_8KHz16bit[] 数组, 数组元素对应wav 的sample 值. 打开该文件, 把数组前后为0或很小的元素删除, 这样占内存小一点.
3. 把PCMdata8K16bit.h 复制到工作目录, 在程序中加入 #include <CMdata8K16bit.h>. 这样在程序中就可直接引用tonePCM_8KHz16bit[] 数组了.
wav2txt.py 程序源码:
import wave
import numpy as np
import sys
wavefile = "Windows Information Bar8k.wav"
textfile = "PCMdata8K16bit.h"
f = wave.open(wavefile, 'rb' )
params = f.getparams()
nchannels, sampwidth, framerate, nframes = params[:4]
print(nchannels)
print(sampwidth)
print(framerate)
print(nframes)
Data_str = f.readframes(nframes)
Data_num = np.frombuffer(Data_str,dtype=np.int16)
datalist = list(Data_num);
f.close()
with open(textfile, 'w+') as outFile:
outFile.write("#include <stdint.h>\n")
outFile.write("int16_t tonePCM_8KHz16bit[] = {")
for index, item in enumerate(datalist):
outFile.write(str(item))
outFile.write(',')
outFile.write('0')
outFile.write('}')
来源:oschina
链接:https://my.oschina.net/u/4418711/blog/4325769