| 1 |
''' |
| 2 |
Documentation: |
| 3 |
http://docs.microsoft.com/en-us/windows/desktop/Multimedia |
| 4 |
|
| 5 |
.. versionadded:: 1.4.0 |
| 6 |
''' |
| 7 |
|
| 8 |
from os.path import join |
| 9 |
|
| 10 |
from ctypes import windll |
| 11 |
from ctypes import ( |
| 12 |
sizeof, c_void_p, c_ulonglong, c_ulong, |
| 13 |
c_wchar_p, byref, Structure, create_string_buffer |
| 14 |
) |
| 15 |
from ctypes.wintypes import DWORD, UINT |
| 16 |
|
| 17 |
from plyer.facades import Audio |
| 18 |
from plyer.platforms.win.storagepath import WinStoragePath |
| 19 |
|
| 20 |
|
| 21 |
ULONG_PTR = c_ulonglong if sizeof(c_void_p) == 8 else c_ulong |
| 22 |
|
| 23 |
|
| 24 |
MCI_OPEN = 0x803 |
| 25 |
MCI_OPEN_TYPE = 0x2000 |
| 26 |
MCI_OPEN_ELEMENT = 512 |
| 27 |
MCI_RECORD = 0x80F |
| 28 |
MCI_STOP = 0x808 |
| 29 |
MCI_SAVE = 0x813 |
| 30 |
MCI_PLAY = 0x806 |
| 31 |
MCI_CLOSE = 0x804 |
| 32 |
|
| 33 |
|
| 34 |
MCI_FROM = 4 |
| 35 |
MCI_TO = 8 |
| 36 |
MCI_WAIT = 2 |
| 37 |
MCI_SAVE_FILE = 256 |
| 38 |
|
| 39 |
|
| 40 |
class MCI_OPEN_PARMS(Structure): |
| 41 |
''' |
| 42 |
Struct for MCI_OPEN message parameters. |
| 43 |
|
| 44 |
.. versionadded:: 1.4.0 |
| 45 |
''' |
| 46 |
|
| 47 |
_fields_ = [ |
| 48 |
('mciOpenParms', ULONG_PTR), |
| 49 |
('wDeviceID', UINT), |
| 50 |
('lpstrDeviceType', c_wchar_p), |
| 51 |
('lpstrElementName', c_wchar_p), |
| 52 |
('lpstrAlias', c_wchar_p) |
| 53 |
] |
| 54 |
|
| 55 |
|
| 56 |
class MCI_RECORD_PARMS(Structure): |
| 57 |
''' |
| 58 |
Struct for MCI_RECORD message parameters. |
| 59 |
|
| 60 |
http://docs.microsoft.com/en-us/windows/desktop/Multimedia/mci-record-parms |
| 61 |
|
| 62 |
.. versionadded:: 1.4.0 |
| 63 |
''' |
| 64 |
|
| 65 |
_fields_ = [ |
| 66 |
('dwCallback', ULONG_PTR), |
| 67 |
('dwFrom', DWORD), |
| 68 |
('dwTo', DWORD) |
| 69 |
] |
| 70 |
|
| 71 |
|
| 72 |
class MCI_SAVE_PARMS(Structure): |
| 73 |
''' |
| 74 |
Struct for MCI_SAVE message parameters. |
| 75 |
|
| 76 |
http://docs.microsoft.com/en-us/windows/desktop/Multimedia/mci-save-parms |
| 77 |
|
| 78 |
.. versionadded:: 1.4.0 |
| 79 |
''' |
| 80 |
|
| 81 |
_fields_ = [ |
| 82 |
('dwCallback', ULONG_PTR), |
| 83 |
('lpfilename', c_wchar_p) |
| 84 |
] |
| 85 |
|
| 86 |
|
| 87 |
class MCI_PLAY_PARMS(Structure): |
| 88 |
''' |
| 89 |
Struct for MCI_PLAY message parameters. |
| 90 |
|
| 91 |
http://docs.microsoft.com/en-us/windows/desktop/Multimedia/mci-play-parms |
| 92 |
|
| 93 |
.. versionadded:: 1.4.0 |
| 94 |
''' |
| 95 |
|
| 96 |
_fields_ = [ |
| 97 |
('dwCallback', ULONG_PTR), |
| 98 |
('dwFrom', DWORD), |
| 99 |
('dwTo', DWORD) |
| 100 |
] |
| 101 |
|
| 102 |
|
| 103 |
def send_command(device, msg, flags, params): |
| 104 |
''' |
| 105 |
Generic mciSendCommandW() wrapper with error handler. |
| 106 |
All parameters are required as for mciSendCommandW(). |
| 107 |
In case of no `params` passed, use `None`, that value |
| 108 |
won't be dereferenced. |
| 109 |
|
| 110 |
.. versionadded:: 1.4.0 |
| 111 |
''' |
| 112 |
|
| 113 |
multimedia = windll.winmm |
| 114 |
send_command_w = multimedia.mciSendCommandW |
| 115 |
get_error = multimedia.mciGetErrorStringW |
| 116 |
|
| 117 |
|
| 118 |
|
| 119 |
|
| 120 |
|
| 121 |
error_len = 128 |
| 122 |
|
| 123 |
|
| 124 |
error_text = create_string_buffer(error_len * 2) |
| 125 |
|
| 126 |
|
| 127 |
error_code = send_command_w( |
| 128 |
device, |
| 129 |
msg, |
| 130 |
flags, |
| 131 |
|
| 132 |
|
| 133 |
|
| 134 |
byref(params) if params else params |
| 135 |
) |
| 136 |
|
| 137 |
|
| 138 |
if error_code: |
| 139 |
|
| 140 |
get_error(error_code, byref(error_text), error_len) |
| 141 |
error_text = error_text.raw.replace(b'\x00', b'').decode('utf-8') |
| 142 |
|
| 143 |
|
| 144 |
|
| 145 |
|
| 146 |
send_command_w(device, MCI_CLOSE, 0, None) |
| 147 |
raise Exception(error_code, error_text) |
| 148 |
|
| 149 |
|
| 150 |
|
| 151 |
return params |
| 152 |
|
| 153 |
|
| 154 |
class WinRecorder: |
| 155 |
''' |
| 156 |
Generic wrapper for MCI_RECORD handling the filenames and device closing |
| 157 |
in the same approach like it is used for other platforms. |
| 158 |
|
| 159 |
.. versionadded:: 1.4.0 |
| 160 |
''' |
| 161 |
|
| 162 |
def __init__(self, device, filename): |
| 163 |
self._device = device |
| 164 |
self._filename = filename |
| 165 |
|
| 166 |
@property |
| 167 |
def device(self): |
| 168 |
''' |
| 169 |
Public property returning device ID. |
| 170 |
|
| 171 |
.. versionadded:: 1.4.0 |
| 172 |
''' |
| 173 |
return self._device |
| 174 |
|
| 175 |
@property |
| 176 |
def filename(self): |
| 177 |
''' |
| 178 |
Public property returning filename for current recording. |
| 179 |
|
| 180 |
.. versionadded:: 1.4.0 |
| 181 |
''' |
| 182 |
return self._filename |
| 183 |
|
| 184 |
def record(self): |
| 185 |
''' |
| 186 |
Start recording a WAV sound. |
| 187 |
|
| 188 |
.. versionadded:: 1.4.0 |
| 189 |
''' |
| 190 |
send_command( |
| 191 |
device=self.device, |
| 192 |
msg=MCI_RECORD, |
| 193 |
flags=0, |
| 194 |
params=None |
| 195 |
) |
| 196 |
|
| 197 |
def stop(self): |
| 198 |
''' |
| 199 |
Stop recording and save the data to a file path |
| 200 |
self.filename. Wait until the file is written. |
| 201 |
Close the device afterwards. |
| 202 |
|
| 203 |
.. versionadded:: 1.4.0 |
| 204 |
''' |
| 205 |
|
| 206 |
|
| 207 |
send_command( |
| 208 |
device=self.device, |
| 209 |
msg=MCI_STOP, |
| 210 |
flags=MCI_WAIT, |
| 211 |
params=None |
| 212 |
) |
| 213 |
|
| 214 |
|
| 215 |
save_params = MCI_SAVE_PARMS() |
| 216 |
save_params.lpfilename = self.filename |
| 217 |
|
| 218 |
|
| 219 |
|
| 220 |
send_command( |
| 221 |
device=self.device, |
| 222 |
msg=MCI_SAVE, |
| 223 |
flags=MCI_SAVE_FILE | MCI_WAIT, |
| 224 |
params=save_params |
| 225 |
) |
| 226 |
|
| 227 |
|
| 228 |
send_command( |
| 229 |
device=self.device, |
| 230 |
msg=MCI_CLOSE, |
| 231 |
flags=0, |
| 232 |
params=None |
| 233 |
) |
| 234 |
|
| 235 |
|
| 236 |
class WinPlayer: |
| 237 |
''' |
| 238 |
Generic wrapper for MCI_PLAY handling the device closing. |
| 239 |
|
| 240 |
.. versionadded:: 1.4.0 |
| 241 |
''' |
| 242 |
|
| 243 |
def __init__(self, device): |
| 244 |
self._device = device |
| 245 |
|
| 246 |
@property |
| 247 |
def device(self): |
| 248 |
''' |
| 249 |
Public property returning device ID. |
| 250 |
|
| 251 |
.. versionadded:: 1.4.0 |
| 252 |
''' |
| 253 |
return self._device |
| 254 |
|
| 255 |
def play(self): |
| 256 |
''' |
| 257 |
Start playing a WAV sound. |
| 258 |
|
| 259 |
.. versionadded:: 1.4.0 |
| 260 |
''' |
| 261 |
play_params = MCI_PLAY_PARMS() |
| 262 |
play_params.dwFrom = 0 |
| 263 |
|
| 264 |
send_command( |
| 265 |
device=self.device, |
| 266 |
msg=MCI_PLAY, |
| 267 |
flags=MCI_FROM, |
| 268 |
params=play_params |
| 269 |
) |
| 270 |
|
| 271 |
def stop(self): |
| 272 |
''' |
| 273 |
Stop playing a WAV sound and close the device. |
| 274 |
|
| 275 |
.. versionadded:: 1.4.0 |
| 276 |
''' |
| 277 |
send_command( |
| 278 |
device=self.device, |
| 279 |
msg=MCI_STOP, |
| 280 |
flags=MCI_WAIT, |
| 281 |
params=None |
| 282 |
) |
| 283 |
|
| 284 |
|
| 285 |
send_command( |
| 286 |
device=self.device, |
| 287 |
msg=MCI_CLOSE, |
| 288 |
flags=0, |
| 289 |
params=None |
| 290 |
) |
| 291 |
|
| 292 |
|
| 293 |
class WinAudio(Audio): |
| 294 |
''' |
| 295 |
Windows implementation of audio recording and audio playing. |
| 296 |
|
| 297 |
.. versionadded:: 1.4.0 |
| 298 |
''' |
| 299 |
|
| 300 |
def __init__(self, file_path=None): |
| 301 |
|
| 302 |
default_path = join( |
| 303 |
WinStoragePath().get_music_dir(), |
| 304 |
'audio.wav' |
| 305 |
) |
| 306 |
super().__init__(file_path or default_path) |
| 307 |
|
| 308 |
self._recorder = None |
| 309 |
self._player = None |
| 310 |
self._current_file = None |
| 311 |
|
| 312 |
def _start(self): |
| 313 |
''' |
| 314 |
Start recording a WAV sound in the background asynchronously. |
| 315 |
|
| 316 |
.. versionadded:: 1.4.0 |
| 317 |
''' |
| 318 |
|
| 319 |
|
| 320 |
|
| 321 |
self._stop() |
| 322 |
|
| 323 |
|
| 324 |
open_params = MCI_OPEN_PARMS() |
| 325 |
open_params.lpstrDeviceType = 'waveaudio' |
| 326 |
open_params.lpstrElementName = '' |
| 327 |
|
| 328 |
|
| 329 |
open_params = send_command( |
| 330 |
device=0, |
| 331 |
msg=MCI_OPEN, |
| 332 |
|
| 333 |
|
| 334 |
|
| 335 |
flags=MCI_OPEN_ELEMENT | MCI_OPEN_TYPE, |
| 336 |
params=open_params |
| 337 |
) |
| 338 |
|
| 339 |
|
| 340 |
self._recorder = WinRecorder( |
| 341 |
device=open_params.wDeviceID, |
| 342 |
filename=self._file_path |
| 343 |
) |
| 344 |
self._recorder.record() |
| 345 |
|
| 346 |
|
| 347 |
|
| 348 |
self._current_file = self._recorder.filename |
| 349 |
|
| 350 |
def _stop(self): |
| 351 |
''' |
| 352 |
Stop recording or playing of a WAV sound. |
| 353 |
|
| 354 |
.. versionadded:: 1.4.0 |
| 355 |
''' |
| 356 |
|
| 357 |
if self._recorder: |
| 358 |
self._recorder.stop() |
| 359 |
self._recorder = None |
| 360 |
|
| 361 |
if self._player: |
| 362 |
self._player.stop() |
| 363 |
self._player = None |
| 364 |
|
| 365 |
def _play(self): |
| 366 |
''' |
| 367 |
Play a WAV sound from a file. Prioritize latest recorded file before |
| 368 |
default file path from WinAudio. |
| 369 |
|
| 370 |
.. versionadded:: 1.4.0 |
| 371 |
''' |
| 372 |
|
| 373 |
|
| 374 |
open_params = MCI_OPEN_PARMS() |
| 375 |
open_params.lpstrDeviceType = 'waveaudio' |
| 376 |
open_params.lpstrElementName = self._current_file or self._file_path |
| 377 |
|
| 378 |
|
| 379 |
open_params = send_command( |
| 380 |
device=0, |
| 381 |
msg=MCI_OPEN, |
| 382 |
|
| 383 |
|
| 384 |
|
| 385 |
flags=MCI_OPEN_ELEMENT | MCI_OPEN_TYPE, |
| 386 |
params=open_params |
| 387 |
) |
| 388 |
|
| 389 |
|
| 390 |
self._player = WinPlayer(device=open_params.wDeviceID) |
| 391 |
self._player.play() |
| 392 |
|
| 393 |
|
| 394 |
def instance(): |
| 395 |
''' |
| 396 |
Instance for facade proxy. |
| 397 |
''' |
| 398 |
return WinAudio() |