forked from gooofy/zamia-ai
-
Notifications
You must be signed in to change notification settings - Fork 0
/
pulseclient.py
160 lines (127 loc) · 4.63 KB
/
pulseclient.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
#!/usr/bin/env python
# -*- coding: utf-8 -*-
#
# Copyright 2013, 2014 Guenter Bartsch
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU Lesser General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU Lesser General Public License for more details.
#
# You should have received a copy of the GNU Lesser General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
#
import StringIO
import wave
import ctypes
import sys
pa = ctypes.cdll.LoadLibrary('libpulse-simple.so.0')
PA_STREAM_PLAYBACK = 1
PA_STREAM_RECORD = 2
PA_SAMPLE_S16LE = 3
class struct_pa_sample_spec(ctypes.Structure):
__slots__ = [
'format',
'rate',
'channels',
]
struct_pa_sample_spec._fields_ = [
('format', ctypes.c_int),
('rate', ctypes.c_uint32),
('channels', ctypes.c_uint8),
]
pa_sample_spec = struct_pa_sample_spec # /usr/include/pulse/sample.h:174
class struct_pa_buffer_attr(ctypes.Structure):
__slots__ = [
'maxlength',
'tlength',
'prebuf',
'minreq',
'fragsize',
]
struct_pa_buffer_attr._fields_ = [
('maxlength', ctypes.c_uint32),
('tlength', ctypes.c_uint32),
('prebuf', ctypes.c_uint32),
('minreq', ctypes.c_uint32),
('fragsize', ctypes.c_uint32),
]
pa_buffer_attr = struct_pa_buffer_attr # /usr/include/pulse/def.h:221
REC_BUF_LEN = 32
class pulserecorder:
def __init__(self, name):
self.name = name
self.buf = ctypes.create_string_buffer(REC_BUF_LEN)
def start(self):
ss = struct_pa_sample_spec()
ss.rate = 48000
ss.channels = 1
ss.format = PA_SAMPLE_S16LE
error = ctypes.c_int(0)
ba = struct_pa_buffer_attr()
ba.maxlength = -1
ba.tlength = -1
ba.prebuf = -1
ba.minreq = -1
ba.fragsize = 48000/10 * 2 # 1/10th of a second
self.s = pa.pa_simple_new(
None, # Default server.
self.name, # Application's name.
PA_STREAM_RECORD, # Stream for recording.
None, # Default device.
'record', # Stream's description.
ctypes.byref(ss), # Sample format.
None, # Default channel map.
ctypes.byref(ba), # buffering attributes.
ctypes.byref(error) # Ignore error code.
)
if not self.s:
raise Exception('Could not create pulse audio stream: {0}!'.format(
pa.strerror(ctypes.byref(error))))
def stop(self):
# Freeing resources and closing connection.
pa.pa_simple_free(self.s)
def record(self):
error = ctypes.c_int(0)
if pa.pa_simple_read(self.s, self.buf, REC_BUF_LEN, error):
raise Exception('Pulseaudio: record failed (pa_simple_read)!')
return (REC_BUF_LEN, self.buf)
class pulseplayer:
def __init__(self, name):
self.name = name
def play(self, buf, len):
ss = struct_pa_sample_spec()
ss.rate = 48000
ss.channels = 1
ss.format = PA_SAMPLE_S16LE
error = ctypes.c_int(0)
s = pa.pa_simple_new(
None, # Default server.
self.name, # Application's name.
PA_STREAM_PLAYBACK, # Stream for playback.
None, # Default device.
'playback', # Stream's description.
ctypes.byref(ss), # Sample format.
None, # Default channel map.
None, # Default buffering attributes.
ctypes.byref(error) # Ignore error code.
)
if not s:
raise Exception('Could not create pulse audio stream: {0}!'.format(pa.strerror(ctypes.byref(error))))
if pa.pa_simple_write(s, buf, len, error):
raise Exception('Could not play file!')
# Waiting for all sent data to finish playing.
if pa.pa_simple_drain(s, error):
raise Exception('Could not simple drain!')
# Freeing resources and closing connection.
pa.pa_simple_free(s)
if __name__ == "__main__":
#player = pulseplayer("HAL 9000")
#player.play(the_sound)
recorder = pulserecorder("HAL 9000")
recorder.record()