-
Notifications
You must be signed in to change notification settings - Fork 0
/
CAHALDevice.py
185 lines (141 loc) · 5.75 KB
/
CAHALDevice.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
from cpaudio_lib import *
from CAHALSampleRateRange import CAHALSampleRateRange
from CAHALDeviceStream import CAHALDeviceStream
class CAHALDevice:
def __init__( self, device ):
if( type( device ) is cahal_device ):
self.struct = device
self.handle = device.handle
self.name = device.device_name
self.model = device.model
self.manufacturer = device.manufacturer
self.serialNumber = device.serial_number
self.version = device.version
self.deviceUID = device.device_uid
self.modelUID = device.model_uid
self.isAlive = ( True if device.is_alive else False )
self.isRunning = ( True if device.is_running else False )
self.preferences = {}
if( device.preferred_number_of_channels ):
self.preferences[ 'numberOfChannels' ] = \
device.preferred_number_of_channels
if( device.preferred_sample_rate ):
self.preferences[ 'sampleRate' ] = device.preferred_sample_rate
self.catalogueSupportedSampleRates( device )
self.catalogueStreams( device )
else:
print "Device is not the correct instance: %s." %( type( device ) )
def catalogueSupportedSampleRates( self, device ):
self.supportedSampleRates = []
if( device.supported_sample_rates ):
sample_rate_index = 0
sample_rate = \
cahal_sample_rate_range_list_get ( \
device.supported_sample_rates, \
sample_rate_index \
)
while( sample_rate ):
sampleRate = \
CAHALSampleRateRange ( \
sample_rate.minimum_rate, \
sample_rate.maximum_rate \
) \
self.supportedSampleRates.append( sampleRate )
sample_rate_index += 1
sample_rate = \
cahal_sample_rate_range_list_get ( \
device.supported_sample_rates, \
sample_rate_index \
)
def catalogueStreams( self, device ):
self.streams = []
if( device.device_streams ):
stream_index = 0
stream = \
cahal_device_stream_list_get ( \
device.device_streams, \
stream_index \
)
while( stream ):
self.streams.append( CAHALDeviceStream( stream ) )
stream_index += 1
stream = \
cahal_device_stream_list_get ( \
device.device_streams, \
stream_index \
)
def doesSupportRecording( self ):
supportsRecording = False
for stream in self.streams:
if( stream.direction == CAHAL_DEVICE_INPUT_STREAM ):
supportsRecording = True
break
return( supportsRecording )
def doesSupportPlayback( self ):
supportsPlayback = False
for stream in self.streams:
if( stream.direction == CAHAL_DEVICE_OUTPUT_STREAM ):
supportsPlayback = True
break
return( supportsPlayback )
def printMe( self ):
print "Device '%s'" %( self.name )
if( self.model ):
print "\tModel: %s" %( self.model )
if( self.manufacturer ):
print "\tManufacturer: %s" %( self.manufacturer )
if( self.serialNumber ):
print "\tSN: %s" %( self.serialNumber )
if( self.version ):
print "\tVersion: %s" %( self.version )
if( self.deviceUID ):
print "\tUID: %s" %( self.deviceUID )
if( self.modelUID ):
print "\tModel UID: %s" %( self.modelUID )
print "\tAlive?: %s" %( "Yes" if self.isAlive else "No" )
print "\tRunning?: %s" %( "Yes" if self.isRunning else "No" )
if ( \
'numberOfChannels' in self.preferences \
and 'sampleRate' in self.preferences \
):
print "\tPreferences:"
print "\t\t%d channel(s), %.02f Hz" \
%( \
self.preferences[ 'numberOfChannels' ], \
self.preferences[ 'sampleRate' ] \
)
if( self.supportedSampleRates ):
print "\tSupported sample rates:"
for sampleRate in self.supportedSampleRates:
sampleRate.printMe( "\t\t" )
if( self.streams ):
print "\tStreams:"
for stream in self.streams:
stream.printMe( "\t\t" )
def doesSupportFormat( self, stream, numberOfChannels, bitDepth, sampleRate ):
for format in stream.supportedFormats:
if( \
format.numberOfChannels == numberOfChannels \
and format.bitDepth == bitDepth \
and \
( \
format.sampleRate.minimum <= sampleRate \
and format.sampleRate.maximum >= sampleRate \
) \
):
return( True )
return( False )
def hasAppropriateStream( self, direction, numberOfChannels, bitDepth, sampleRate ):
foundStream = False
for stream in self.streams:
if( \
stream.direction == direction \
and self.doesSupportFormat ( \
stream, \
numberOfChannels, \
bitDepth, \
sampleRate \
) \
):
foundStream = True
return( foundStream )