rahul dhir
rahul dhir

Reputation: 29

I want to convert my data read from a wave file into a byte array using python

import wave as wv 
import numpy as np 
import struct 
wavefile=wv.open("/home/rahul/python/sample.wav",'rb')(nchannels,samplewidth,framerate,nframes,comptype,compname)=wavefile.getparams()
wavedata=wavefile.readframes(2) 
print "channels:",nchannels
print "sample width:",samplewidth
print "framerate:",framerate
print "no of frames:",nframes 
print "comptype:",comptype
print "compname:",compname 
wavefile.close() 

the Output is::

channels:1
samplewidth:2
framerate:44100
no of frames:220500
comptype:NONE
compname:not compressed

This is necessary to be into byte array because i have to read them 3 bytes at a time,split them into 4 parts of 6 bits each and them assign them characters according to a reference table so as to convert into text file and then compress this text file

Upvotes: 1

Views: 3364

Answers (1)

Anil_M
Anil_M

Reputation: 11443

Here's one way you can get wav file into Byte array.

import array
byte_array = array.array('B')
audio_file = open("buh.wav", 'rb')
byte_array.fromstring(audio_file.read())
print len(byte_array)
audio_file.close()

The file size was 52Kb

>>> len(byte_array)
53054

Read 3 bytes

>>> byte_array[0:3]
array('B', [82, 73, 70])

Upvotes: 2

Related Questions