-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathminima.pyx
165 lines (116 loc) · 5.02 KB
/
minima.pyx
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
from enum import Enum
cimport libminiaudio as lib
DEF MA_NO_DECODING = 1
DEF MA_NO_ENCODING = 1
DEF DEVICE_CHANNELS = 2
DEF DEVICE_SAMPLE_RATE = 48000
def get_version() -> str:
cdef const char* version = lib.ma_version_string()
return version.decode()
cdef void sine_data_callback(lib.ma_device* device,
void* output,
const void* input_,
lib.ma_uint32 frame_count) noexcept nogil:
"""callback for `play_sine` function"""
cdef lib.ma_waveform* sinewave
sinewave = <lib.ma_waveform*>device.pUserData
lib.ma_waveform_read_pcm_frames(sinewave, output, frame_count, NULL)
def play_sine(double amp=0.2, double freq=220):
"""generate and play a sign wave"""
cdef lib.ma_waveform sineWave
cdef lib.ma_device_config deviceConfig
cdef lib.ma_device device
cdef lib.ma_waveform_config sineWaveConfig
sineWaveConfig = lib.ma_waveform_config_init(
lib.ma_format_f32,
DEVICE_CHANNELS, DEVICE_SAMPLE_RATE,
lib.ma_waveform_type_sine, amp, freq)
lib.ma_waveform_init(&sineWaveConfig, &sineWave)
deviceConfig = lib.ma_device_config_init(lib.ma_device_type_playback)
deviceConfig.playback.format = lib.ma_format_f32
deviceConfig.playback.channels = DEVICE_CHANNELS
deviceConfig.sampleRate = DEVICE_SAMPLE_RATE
deviceConfig.dataCallback = sine_data_callback
deviceConfig.pUserData = &sineWave
if (lib.ma_device_init(NULL, &deviceConfig, &device) != lib.MA_SUCCESS):
print("Failed to open playback device.")
return -4
print("Device Name: %s" % device.playback.name.decode())
if (lib.ma_device_start(&device) != lib.MA_SUCCESS):
print("Failed to start playback device.\n")
lib.ma_device_uninit(&device)
return -5
if input("Press Enter to quit...\n") == '':
lib.ma_device_uninit(&device)
cdef void file_data_callback(lib.ma_device* device,
void* output, const void* input_,
lib.ma_uint32 frame_count) noexcept nogil:
cdef lib.ma_decoder* decoder = <lib.ma_decoder*>device.pUserData
if (decoder == NULL):
return
lib.ma_decoder_read_pcm_frames(decoder, output, frame_count, NULL)
def play_file(str path):
"""play a given audio file on the default device"""
cdef lib.ma_result result
cdef lib.ma_decoder decoder
cdef lib.ma_device_config deviceConfig
cdef lib.ma_device device
result = lib.ma_decoder_init_file(path.encode('utf8'), NULL, &decoder)
if (result != lib.MA_SUCCESS):
return
deviceConfig = lib.ma_device_config_init(lib.ma_device_type_playback)
deviceConfig.playback.format = decoder.outputFormat
deviceConfig.playback.channels = decoder.outputChannels
deviceConfig.sampleRate = decoder.outputSampleRate
deviceConfig.dataCallback = file_data_callback
deviceConfig.pUserData = &decoder
if (lib.ma_device_init(NULL, &deviceConfig, &device) != lib.MA_SUCCESS):
print("Failed to open playback device.")
lib.ma_decoder_uninit(&decoder)
return
if (lib.ma_device_start(&device) != lib.MA_SUCCESS):
print("Failed to start playback device.")
lib.ma_device_uninit(&device)
lib.ma_decoder_uninit(&decoder)
return
if input("Press Enter to quit...\n") == '':
lib.ma_device_uninit(&device)
lib.ma_decoder_uninit(&decoder)
def list_devices():
"""enumerates the available devices detected by miniaudio"""
cdef lib.ma_result result
cdef lib.ma_context context
cdef lib.ma_device_info* pPlaybackDeviceInfos
cdef lib.ma_uint32 playbackDeviceCount
cdef lib.ma_device_info* pCaptureDeviceInfos
cdef lib.ma_uint32 captureDeviceCount
cdef lib.ma_uint32 i = 0
space = " "*4
if (lib.ma_context_init(NULL, 0, NULL, &context) != lib.MA_SUCCESS):
print("Failed to initialize context.")
return
result = lib.ma_context_get_devices(
&context,
&pPlaybackDeviceInfos, &playbackDeviceCount,
&pCaptureDeviceInfos, &captureDeviceCount)
if (result != lib.MA_SUCCESS):
print("Failed to retrieve device information.")
return
print("Playback Devices:")
for i in range(0, playbackDeviceCount):
print(space, i, pPlaybackDeviceInfos[i].name.decode())
print("Capture Devices:")
for i in range(0, captureDeviceCount):
print(space, i, pCaptureDeviceInfos[i].name.decode())
lib.ma_context_uninit(&context)
def engine_play_file(str filename):
"""demonstrates how to initialize an audio engine and play a sound."""
cdef lib.ma_result result
cdef lib.ma_engine engine
result = lib.ma_engine_init(NULL, &engine)
if (result != lib.MA_SUCCESS):
print("Failed to initialize audio engine.")
return
lib.ma_engine_play_sound(&engine, filename.encode('utf8'), NULL)
if input("Press Enter to quit...\n") == '':
lib.ma_engine_uninit(&engine)