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