python wav 转c 数组 、bin功能实现-凯发app官方网站

凯发app官方网站-凯发k8官网下载客户端中心 | | 凯发app官方网站-凯发k8官网下载客户端中心
  • 博客访问: 3574026
  • 博文数量: 365
  • 博客积分: 0
  • 博客等级: 民兵
  • 技术积分: 2522
  • 用 户 组: 普通用户
  • 注册时间: 2019-10-28 13:40
文章分类

(365)

  • (365)
文章存档

(8)

(130)

(155)

(50)

(22)

我的朋友
相关博文
  • ·
  • ·
  • ·
  • ·
  • ·
  • ·
  • ·
  • ·
  • ·
  • ·

分类: python/ruby

2021-07-06 17:29:07

import wave

import argparse

import os.path

import struct

#prepare an argument parser

parser = argparse.argumentparser(description='convert .wav to .bin or c-style array.')

parser.add_argument('input')

parser.add_argument('-o',metavar = '')

parser.add_argument('-b',action='store_true',help='generate .bin file.',default=true)

parser.add_argument('-a',action='store_true',help='generate .txt(c-style array source) file.')

parser.add_argument('-r',action='store_true',help='reduce bit resorution from 16-bit to 8-bit')

pres = parser.parse_args()

#set output filename

if pres.o is none:

    fwname = pres.input

else:

    fwname = pres.o

#set output file's extension and open file

if pres.b:

    fbin = open(fwname '.bin','wb')

if pres.a:

    farr = open(fwname '.txt','w')

#open the wave file

if os.path.exists(pres.input):

    wav = wave.open(pres.input,'r')

    winfo = wav.getparams()

    print('filename: ' pres.input)

    print('number of channels: ' str(winfo[0]) ' [ch]')

    print('number of frame: ' str(winfo[3]) ' [frames]')

    print('size of audio data: ' str(winfo[3] * winfo[1] * winfo[0]) ' [bytes]')

    print('sampling rate: ' str(winfo[2]) ' [hz]')

    print('sample size: ' str(winfo[1]) ' [bytes/sample]')

    print('compression: ' str(winfo[5]))

else:

    print('error:no such file ' '\'' pres.input '\'')

    quit()

#prepare the header for c-style option

if pres.a:

    farr.write('/*\n')

    farr.write('filename: ' pres.input)

    farr.write('file format: ' str(winfo[0]) '-ch,' str(winfo[1] * 8) '-bit,' str(winfo[2]) 'hz\n')

    if pres.r:

        farr.write('bit resolution is reduced to 8-bit.\n')

    farr.write('*/\n')

    #calc number of element

    noe = winfo[3] * winfo[0]

    if winfo[1] == 2 and pres.r is not true:#for 16bit wav without -r option

        farr.write('int16_t wave[' str(noe) ']={\n')

    else:

        farr.write('uint8_t wave[' str(noe) ']={\n')

#write the file

for i in range(winfo[3]):

#for i in range(1):

    t = wav.readframes(1)

    if winfo[1] == 2:# if bytes/sample are 2 bytes (16-bit)

        if pres.r:# bit reduce option

            dl = struct.unpack('h',t[0:2])[0]

            dl = int(dl / 256.0) 128

            if winfo[0] == 2:# if file has two channel

                dr = struct.unpack('h',t[2:4])[0]

                dr = int(dr / 256.0) 128

        else: #treated as unsigned 16bit int

            dl = struct.unpack('h',t[0:2])[0]

            if winfo[0] == 2:# if file has two channel

                dr = struct.unpack('h',t[2:4])[0]

    else:# if bytes/sample is 1 byte (8-bit)

        dl = t[0]

        if winfo[0] == 2:# if file has two channel

            dr = t[1]

    if pres.b:#binary output

        if pres.r:

            if winfo[0] == 2:# if file has two channel

                fbin.write(struct.pack('bb',dl,dr))

            else:

                fbin.write(struct.pack('b',dl))

        else:

            fbin.write(t)

    if pres.a:#c-style array output

        if winfo[1] == 2 and pres.r is not true:#for 16bit wav without -r option

            if winfo[0] == 2:

                farr.write('0x{0:0>4x},0x{1:0>4x}'.format(dl,dr))

            else:

                farr.write('0x{0:0>4x}'.format(dl))

        else:

            if winfo[0] == 2:

                farr.write('0x{0:0>2x},0x{1:0>2x}'.format(dl,dr))

            else:

                farr.write('0x{0:0>2x}'.format(dl))

        #print comma

        if i != (winfo[3] - 1):

            farr.write(',')

        #print new line

        if winfo[0] == 2 and (i % 4) == 3:

            farr.write('\n')

        elif winfo[0] == 1 and (i % 8) == 7:

            farr.write('\n')

if pres.a: #close the array

    farr.write('};\n')

阅读(2377) | 评论(0) | 转发(0) |
0

上一篇:

下一篇:

给主人留下些什么吧!~~
")); function link(t){ var href= $(t).attr('href'); href ="?url=" encodeuricomponent(location.href); $(t).attr('href',href); //setcookie("returnouturl", location.href, 60, "/"); }
网站地图