PythonExamples

all about robots

PythonExamples

Postby chris on Sat Dec 08, 2012 7:49 pm

8 Bit Wave File Converter to AVR PGM Memory

Code: Select all
'''
Created on 11.05.2013

    convert an 8 bit wav file into a c-array

@author: chris
'''
import wave, struct

waveFile = wave.open('g-piano4.wav', 'r')

length = waveFile.getnframes()
dat=[]
for i in range(0,length):
    waveData = waveFile.readframes(1)
    data = struct.unpack("B", waveData)# B ==> 8bit data, change this line for other formats
    dat.append(data[0])


print dat

f = open('wave.c', 'w')
f.write('const uint8_t wave['+str(len(dat))+']={')

for n in range(len(dat)-1):
    if n%32==0:
        f.write('\n')
    x=dat[n]
    f.write(str(int(x))+' ,')

n+=1
x=dat[n]
f.write(str(int(x))+'\n')
f.write('};')   
f.close()

chris
 
Posts: 289
Joined: Sun May 25, 2008 7:21 am

Return to robots

Who is online

Users browsing this forum: No registered users and 1 guest

cron