annotate trunk/swig/aimc_data_io.py @ 448:eba6d914f82c

corrected a bug with stereo files in FileInput, and some more stuff
author hamel.phil
date Thu, 06 Jan 2011 03:33:11 +0000
parents
children 913d13255f61
rev   line source
hamel@448 1 #!/usr/bin/env python
hamel@448 2 #
hamel@448 3 # audio_example.py
hamel@448 4 #
hamel@448 5 #
hamel@448 6 # Created by Philippe Hamel (hamel.phil@gmail.com) on 11-01-05.
hamel@448 7 #
hamel@448 8 # methods to read and write aimc data files
hamel@448 9
hamel@448 10 from struct import *
hamel@448 11 import numpy as N
hamel@448 12
hamel@448 13 #int_size = calcsize('i')
hamel@448 14 #float_size = calcsize('d')
hamel@448 15
hamel@448 16 def readbin(type,file) :
hamel@448 17 """
hamel@448 18 used to read binary data from a file
hamel@448 19 """
hamel@448 20 return unpack(type,file.read(calcsize(type)))
hamel@448 21
hamel@448 22 def read_aimc_data(filename):
hamel@448 23 file = open(filename,'rb')
hamel@448 24
hamel@448 25 nFrames = readbin('i',file)[0]
hamel@448 26 period = readbin( 'f', file)[0] # Frame period in ms
hamel@448 27 nChannels = readbin( 'i', file)[0] # vertical axis of an AI
hamel@448 28 nSamples = readbin( 'i', file)[0] # horizontal axis of an AI
hamel@448 29 sample_rate = readbin('f', file)[0] # sample rate of each channel in Hz
hamel@448 30
hamel@448 31 print 'nFrames, period, nChannels, nSamples, sample_rate'
hamel@448 32 print nFrames, period, nChannels, nSamples, sample_rate
hamel@448 33
hamel@448 34 nData = nFrames * nChannels * nSamples
hamel@448 35 print nData
hamel@448 36 vec_data = readbin('%if'%nData,file)
hamel@448 37
hamel@448 38 file.close()
hamel@448 39 data = N.reshape(vec_data,(nFrames, nChannels, nSamples))
hamel@448 40 return data, nFrames, period, nChannels, nSamples, sample_rate
hamel@448 41
hamel@448 42 #TODO write_aimc_data(data, nFrames, period, nChannels, nSamples, sample_rate)