GHIElectronics.Endpoint.Devices.Sound 0.5.3__py3-none-any.whl
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- GHIElectronics/Endpoint/Devices/Sound/__init__.py +163 -0
- ghielectronics_endpoint_devices_sound-0.5.3.dist-info/METADATA +20 -0
- ghielectronics_endpoint_devices_sound-0.5.3.dist-info/RECORD +5 -0
- ghielectronics_endpoint_devices_sound-0.5.3.dist-info/WHEEL +5 -0
- ghielectronics_endpoint_devices_sound-0.5.3.dist-info/top_level.txt +1 -0
|
@@ -0,0 +1,163 @@
|
|
|
1
|
+
"""Sound of Endpoint, with the API of .NET IoT's SoundDevice (Iot.Device.Media):
|
|
2
|
+
play WAV and MP3, record WAV, volume and mute, on any sound card of the board.
|
|
3
|
+
|
|
4
|
+
from GHIElectronics.Endpoint.Devices.Sound import SoundConnectionSettings, SoundDevice
|
|
5
|
+
|
|
6
|
+
with SoundDevice.Create(SoundConnectionSettings()) as sound: # the first sound card
|
|
7
|
+
sound.Play("/media/usb0/beep.wav")
|
|
8
|
+
sound.PlayMp3("/media/usb0/song.mp3")
|
|
9
|
+
sound.Record(5, "/data/voice.wav") # 5 s from the microphone
|
|
10
|
+
|
|
11
|
+
A USB sound card is "hw:1" (the DeviceName of its UsbHostController event):
|
|
12
|
+
with more than one card, give it as PlaybackDeviceName, RecordingDeviceName and
|
|
13
|
+
MixerDeviceName ("hw:" names are opened as "plughw:", which converts any WAV
|
|
14
|
+
to what the card plays). Play, PlayMp3 and Record wait until they are done.
|
|
15
|
+
Volumes are in the card's own units, as on .NET.
|
|
16
|
+
"""
|
|
17
|
+
|
|
18
|
+
import ctypes
|
|
19
|
+
import os
|
|
20
|
+
|
|
21
|
+
from GHIElectronics.Endpoint.Core._native import native
|
|
22
|
+
|
|
23
|
+
__all__ = ["SoundConnectionSettings", "SoundDevice"]
|
|
24
|
+
|
|
25
|
+
|
|
26
|
+
class SoundConnectionSettings:
|
|
27
|
+
"""Which sound card plays, records and has the volume controls, and how recordings sound"""
|
|
28
|
+
|
|
29
|
+
def __init__(self):
|
|
30
|
+
self.PlaybackDeviceName = "default"
|
|
31
|
+
self.RecordingDeviceName = "default"
|
|
32
|
+
self.MixerDeviceName = "default"
|
|
33
|
+
self.RecordingSampleRate = 8000
|
|
34
|
+
self.RecordingChannels = 2
|
|
35
|
+
self.RecordingBitsPerSample = 16
|
|
36
|
+
|
|
37
|
+
def __repr__(self):
|
|
38
|
+
return (f"SoundConnectionSettings(PlaybackDeviceName={self.PlaybackDeviceName!r}, "
|
|
39
|
+
f"RecordingDeviceName={self.RecordingDeviceName!r}, MixerDeviceName={self.MixerDeviceName!r}, "
|
|
40
|
+
f"RecordingSampleRate={self.RecordingSampleRate}, RecordingChannels={self.RecordingChannels}, "
|
|
41
|
+
f"RecordingBitsPerSample={self.RecordingBitsPerSample})")
|
|
42
|
+
|
|
43
|
+
|
|
44
|
+
def _is_path(value):
|
|
45
|
+
return isinstance(value, (str, os.PathLike))
|
|
46
|
+
|
|
47
|
+
|
|
48
|
+
class SoundDevice:
|
|
49
|
+
@staticmethod
|
|
50
|
+
def Create(settings, unmute=True):
|
|
51
|
+
"""A sound device; unmute: turn the sound on (playback and recording), as .NET does"""
|
|
52
|
+
return SoundDevice(settings, unmute)
|
|
53
|
+
|
|
54
|
+
def __init__(self, settings, unmute=True):
|
|
55
|
+
self._settings = settings
|
|
56
|
+
handle = native.out_handle()
|
|
57
|
+
native.sound_create(settings.PlaybackDeviceName.encode(), settings.RecordingDeviceName.encode(),
|
|
58
|
+
settings.MixerDeviceName.encode(), int(settings.RecordingSampleRate),
|
|
59
|
+
int(settings.RecordingChannels), int(settings.RecordingBitsPerSample), 1 if unmute else 0,
|
|
60
|
+
native.ref(handle))
|
|
61
|
+
self._handle = handle
|
|
62
|
+
|
|
63
|
+
@property
|
|
64
|
+
def Settings(self):
|
|
65
|
+
return self._settings
|
|
66
|
+
|
|
67
|
+
def _h(self):
|
|
68
|
+
if self._handle is None:
|
|
69
|
+
raise native.disposed("SoundDevice")
|
|
70
|
+
return self._handle
|
|
71
|
+
|
|
72
|
+
def _volume(self, capture):
|
|
73
|
+
volume = native.out_int64()
|
|
74
|
+
native.sound_get_volume(self._h(), capture, native.ref(volume))
|
|
75
|
+
return volume.value
|
|
76
|
+
|
|
77
|
+
def _mute(self, capture):
|
|
78
|
+
mute = native.out_int()
|
|
79
|
+
native.sound_get_mute(self._h(), capture, native.ref(mute))
|
|
80
|
+
return bool(mute.value)
|
|
81
|
+
|
|
82
|
+
@property
|
|
83
|
+
def PlaybackVolume(self):
|
|
84
|
+
"""The card's playback volume, in its own units"""
|
|
85
|
+
return self._volume(0)
|
|
86
|
+
|
|
87
|
+
@PlaybackVolume.setter
|
|
88
|
+
def PlaybackVolume(self, value):
|
|
89
|
+
native.sound_set_volume(self._h(), 0, int(value))
|
|
90
|
+
|
|
91
|
+
@property
|
|
92
|
+
def PlaybackMute(self):
|
|
93
|
+
return self._mute(0)
|
|
94
|
+
|
|
95
|
+
@PlaybackMute.setter
|
|
96
|
+
def PlaybackMute(self, value):
|
|
97
|
+
native.sound_set_mute(self._h(), 0, 1 if value else 0)
|
|
98
|
+
|
|
99
|
+
@property
|
|
100
|
+
def RecordingVolume(self):
|
|
101
|
+
"""The card's recording volume, in its own units"""
|
|
102
|
+
return self._volume(1)
|
|
103
|
+
|
|
104
|
+
@RecordingVolume.setter
|
|
105
|
+
def RecordingVolume(self, value):
|
|
106
|
+
native.sound_set_volume(self._h(), 1, int(value))
|
|
107
|
+
|
|
108
|
+
@property
|
|
109
|
+
def RecordingMute(self):
|
|
110
|
+
return self._mute(1)
|
|
111
|
+
|
|
112
|
+
@RecordingMute.setter
|
|
113
|
+
def RecordingMute(self, value):
|
|
114
|
+
native.sound_set_mute(self._h(), 1, 1 if value else 0)
|
|
115
|
+
|
|
116
|
+
def _play(self, source, mp3):
|
|
117
|
+
if _is_path(source):
|
|
118
|
+
native.sound_play_file(self._h(), os.fsencode(source), mp3)
|
|
119
|
+
else:
|
|
120
|
+
data = source.read() if hasattr(source, "read") else bytes(source)
|
|
121
|
+
pointer, length, keep = native.readable(data)
|
|
122
|
+
native.sound_play_buffer(self._h(), pointer, length, mp3)
|
|
123
|
+
|
|
124
|
+
def Play(self, wav):
|
|
125
|
+
"""Plays a WAV: a file's path, an open file (binary) or its bytes"""
|
|
126
|
+
self._play(wav, 0)
|
|
127
|
+
|
|
128
|
+
def PlayMp3(self, mp3):
|
|
129
|
+
"""Plays an MP3: a file's path, an open file (binary) or its bytes"""
|
|
130
|
+
self._play(mp3, 1)
|
|
131
|
+
|
|
132
|
+
def Record(self, recordTimeSeconds, output):
|
|
133
|
+
"""Records a WAV for the given seconds into a file's path or an open file (binary)"""
|
|
134
|
+
if _is_path(output):
|
|
135
|
+
native.sound_record_file(self._h(), int(recordTimeSeconds), os.fsencode(output))
|
|
136
|
+
return
|
|
137
|
+
s = self._settings
|
|
138
|
+
size = 44 + int(recordTimeSeconds) * s.RecordingSampleRate * s.RecordingChannels * s.RecordingBitsPerSample // 8
|
|
139
|
+
buffer = ctypes.create_string_buffer(size)
|
|
140
|
+
length = native.out_int()
|
|
141
|
+
native.sound_record_buffer(self._h(), int(recordTimeSeconds), buffer, size, native.ref(length))
|
|
142
|
+
output.write(buffer.raw[:length.value])
|
|
143
|
+
|
|
144
|
+
def StartRecording(self, outputFilePath):
|
|
145
|
+
"""Records a WAV file in the background until StopRecording"""
|
|
146
|
+
native.sound_start_recording(self._h(), os.fsencode(outputFilePath))
|
|
147
|
+
|
|
148
|
+
def StopRecording(self):
|
|
149
|
+
native.sound_stop_recording(self._h())
|
|
150
|
+
|
|
151
|
+
def Dispose(self):
|
|
152
|
+
if self._handle is not None:
|
|
153
|
+
native.sound_dispose(self._handle)
|
|
154
|
+
self._handle = None
|
|
155
|
+
|
|
156
|
+
def __enter__(self):
|
|
157
|
+
return self
|
|
158
|
+
|
|
159
|
+
def __exit__(self, *exception):
|
|
160
|
+
self.Dispose()
|
|
161
|
+
|
|
162
|
+
def __del__(self):
|
|
163
|
+
self.Dispose()
|
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
Metadata-Version: 2.4
|
|
2
|
+
Name: GHIElectronics.Endpoint.Devices.Sound
|
|
3
|
+
Version: 0.5.3
|
|
4
|
+
Summary: Sound (play and record WAV / MP3) for GHI Electronics Endpoint, with the API of .NET IoT's SoundDevice.
|
|
5
|
+
Author-email: "GHI Electronics, LLC" <support@ghielectronics.com>
|
|
6
|
+
License-Expression: MIT
|
|
7
|
+
Project-URL: Homepage, https://www.ghielectronics.com/
|
|
8
|
+
Project-URL: Repository, https://github.com/ghi-electronics/Endpoint-Libraries
|
|
9
|
+
Project-URL: Issues, https://github.com/ghi-electronics/Endpoint-Libraries/issues
|
|
10
|
+
Classifier: Programming Language :: Python :: 3
|
|
11
|
+
Classifier: Operating System :: POSIX :: Linux
|
|
12
|
+
Requires-Python: >=3.9
|
|
13
|
+
Description-Content-Type: text/markdown
|
|
14
|
+
Requires-Dist: GHIElectronics.Endpoint.Core<0.6,>=0.5.3
|
|
15
|
+
|
|
16
|
+
# GHIElectronics.Endpoint.Devices.Sound
|
|
17
|
+
|
|
18
|
+
Sound (play and record WAV / MP3) for GHI Electronics Endpoint, with the API of .NET IoT's SoundDevice.
|
|
19
|
+
|
|
20
|
+
Part of the [GHI Electronics Endpoint](https://github.com/ghi-electronics/Endpoint-Libraries) libraries. See the repository README for the full package list and getting-started guide.
|
|
@@ -0,0 +1,5 @@
|
|
|
1
|
+
GHIElectronics/Endpoint/Devices/Sound/__init__.py,sha256=V3aAwUecwzPnd7M8usguJCc_mNoYUU1qp4eC6pYOzbg,5931
|
|
2
|
+
ghielectronics_endpoint_devices_sound-0.5.3.dist-info/METADATA,sha256=A4TSkj0nyUC37lV6p-h0zKQzg3g6bOGUG0n2fqGkMDc,1065
|
|
3
|
+
ghielectronics_endpoint_devices_sound-0.5.3.dist-info/WHEEL,sha256=mOdXR9C3wMVov7cSY8PY-XCzo3SmmWCHvGQr5csJoao,91
|
|
4
|
+
ghielectronics_endpoint_devices_sound-0.5.3.dist-info/top_level.txt,sha256=NpOZNgd0iquwrRPrTOhUDxc2V9TtNFLv5Z_Dx3YZtIU,15
|
|
5
|
+
ghielectronics_endpoint_devices_sound-0.5.3.dist-info/RECORD,,
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
GHIElectronics
|