annotate swig/aimc_data_io.py @ 150:9425901c60a6

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