1- #define DR_FLAC_IMPLEMENTATION
2- #define DR_MP3_IMPLEMENTATION
3- #define DR_WAV_IMPLEMENTATION
4- #define MINIAUDIO_IMPLEMENTATION
1+ #define MINIAUDIO_IMPLEMENTATION
2+
3+ #include " library.h"
54
6- #include " Submodules/miniaudio/miniaudio.h"
75
86#define LIBRARY_H
97
108// Helper macro for memory allocation.
119#define sf_create (t ) (t*) ma_malloc(sizeof (t), NULL )
1210
13- void sf_debug (const char *msg, ...) {
14- va_list args;
15- va_start (args, msg);
16-
17- // SF: Print
18- freopen (" native_output.txt" , " a" , stdout); // Redirect stdout to a file
19- vprintf (msg, args);
20- fclose (stdout);
21-
22- va_end (args);
23- }
24-
2511extern " C" {
2612// Frees a structure allocated with sf_create().
2713MA_API void sf_free (void *ptr) {
@@ -43,10 +29,17 @@ MA_API ma_device *sf_allocate_device() {
4329 return sf_create (ma_device);
4430}
4531
32+ // Allocate memory for a context struct.
33+ MA_API ma_context *sf_allocate_context () {
34+ return sf_create (ma_context);
35+ }
36+
4637// Allocate memory for a device configuration struct.
4738MA_API ma_device_config *sf_allocate_device_config (const ma_device_type deviceType, const ma_format format,
4839 const ma_uint32 channels, const ma_uint32 sampleRate,
49- const ma_device_data_proc dataCallback) {
40+ const ma_device_data_proc dataCallback,
41+ const ma_device_id *playbackDeviceId,
42+ const ma_device_id *captureDeviceId) {
5043 auto *config = sf_create (ma_device_config);
5144 if (config == nullptr ) {
5245 return nullptr ;
@@ -65,6 +58,9 @@ MA_API ma_device_config *sf_allocate_device_config(const ma_device_type deviceTy
6558 config->capture .format = format;
6659 config->capture .channels = channels;
6760
61+ // Set device IDs
62+ config->playback .pDeviceID = playbackDeviceId;
63+ config->capture .pDeviceID = captureDeviceId;
6864
6965 return config;
7066}
@@ -80,7 +76,6 @@ MA_API ma_decoder_config *sf_allocate_decoder_config(const ma_format outputForma
8076 MA_ZERO_OBJECT (pConfig);
8177 *pConfig = ma_decoder_config_init (outputFormat, outputChannels, outputSampleRate);
8278
83-
8479 return pConfig;
8580}
8681
@@ -98,18 +93,133 @@ MA_API ma_encoder_config *sf_allocate_encoder_config(const ma_encoding_format en
9893 return pConfig;
9994}
10095
101- // Seek current stream position to a specific offset (necessary to call ma_decoder_seek_to_pcm_frame from native code because of invoking issue).
102- MA_API ma_result sf_decoder_seek_to_frame (ma_decoder *decoder, const ma_uint64 frameIndex) {
103- return ma_decoder_seek_to_pcm_frame (decoder, frameIndex);
96+ ma_result sf_get_devices (ma_context *context, sf_device_info **ppPlaybackDeviceInfos,
97+ sf_device_info **ppCaptureDeviceInfos, ma_uint32 *pPlaybackDeviceCount,
98+ ma_uint32 *pCaptureDeviceCount) {
99+ ma_device_info *pPlaybackDevices = nullptr ;
100+ ma_device_info *pCaptureDevices = nullptr ;
101+
102+ const auto result = ma_context_get_devices (context,
103+ &pPlaybackDevices,
104+ pPlaybackDeviceCount,
105+ &pCaptureDevices,
106+ pCaptureDeviceCount);
107+
108+ if (result != MA_SUCCESS || *pPlaybackDeviceCount == 0 && *pCaptureDeviceCount == 0 ) {
109+ return result;
110+ }
111+
112+ sf_device_info *playbackDeviceInfos = nullptr ; // Local variables, better names
113+ sf_device_info *captureDeviceInfos = nullptr ;
114+
115+ if (*pPlaybackDeviceCount > 0 ) {
116+ playbackDeviceInfos = static_cast <sf_device_info *>(ma_malloc (sizeof (sf_device_info) * *pPlaybackDeviceCount,
117+ nullptr ));
118+ if (playbackDeviceInfos == nullptr ) {
119+ sf_free (pPlaybackDevices);
120+ sf_free (pCaptureDevices);
121+ return MA_OUT_OF_MEMORY;
122+ }
123+ }
124+
125+ if (*pCaptureDeviceCount > 0 ) {
126+ captureDeviceInfos = static_cast <sf_device_info *>(ma_malloc (sizeof (sf_device_info) * *pCaptureDeviceCount,
127+ nullptr ));
128+ if (captureDeviceInfos == nullptr ) {
129+ sf_free (pPlaybackDevices);
130+ sf_free (pCaptureDevices);
131+ sf_free (playbackDeviceInfos);
132+ return MA_OUT_OF_MEMORY;
133+ }
134+ }
135+
136+ for (ma_uint32 iDevice = 0 ; iDevice < *pPlaybackDeviceCount; ++iDevice) {
137+ auto *pPlaybackDeviceInfo = &pPlaybackDevices[iDevice];
138+ sf_device_info deviceInfo;
139+ deviceInfo.id = &pPlaybackDeviceInfo->id ;
140+ strncpy (deviceInfo.name , pPlaybackDeviceInfo->name , MA_MAX_DEVICE_NAME_LENGTH); // Use strncpy
141+ deviceInfo.name [MA_MAX_DEVICE_NAME_LENGTH] = ' \0 ' ; // Ensure null termination
142+ deviceInfo.isDefault = pPlaybackDeviceInfo->isDefault ;
143+ deviceInfo.nativeDataFormatCount = pPlaybackDeviceInfo->nativeDataFormatCount ;
144+
145+ if (deviceInfo.nativeDataFormatCount > 0 ) {
146+ // Allocate memory for nativeDataFormats
147+ deviceInfo.nativeDataFormats = static_cast <native_data_format *>(ma_malloc (
148+ sizeof (native_data_format) * pPlaybackDeviceInfo->nativeDataFormatCount ,
149+ nullptr ));
150+
151+ // Copy all nativeDataFormats
152+ for (ma_uint32 iFormat = 0 ; iFormat < pPlaybackDeviceInfo->nativeDataFormatCount ; ++iFormat) {
153+ deviceInfo.nativeDataFormats [iFormat].format = pPlaybackDeviceInfo->nativeDataFormats [iFormat].format ;
154+ deviceInfo.nativeDataFormats [iFormat].channels = pPlaybackDeviceInfo->nativeDataFormats [iFormat].
155+ channels;
156+ deviceInfo.nativeDataFormats [iFormat].sampleRate = pPlaybackDeviceInfo->nativeDataFormats [iFormat].
157+ sampleRate;
158+ deviceInfo.nativeDataFormats [iFormat].flags = pPlaybackDeviceInfo->nativeDataFormats [iFormat].flags ;
159+ }
160+ }
161+
162+ if (playbackDeviceInfos != nullptr )
163+ playbackDeviceInfos[iDevice] = deviceInfo;
164+ }
165+
166+ for (ma_uint32 iDevice = 0 ; iDevice < *pCaptureDeviceCount; ++iDevice) {
167+ auto *pCaptureDeviceInfo = &pCaptureDevices[iDevice];
168+ sf_device_info deviceInfo;
169+ deviceInfo.id = &pCaptureDeviceInfo->id ;
170+ strncpy (deviceInfo.name , pCaptureDeviceInfo->name , MA_MAX_DEVICE_NAME_LENGTH);
171+ deviceInfo.name [MA_MAX_DEVICE_NAME_LENGTH] = ' \0 ' ; // Ensure null termination
172+ deviceInfo.isDefault = pCaptureDeviceInfo->isDefault ;
173+ deviceInfo.nativeDataFormatCount = pCaptureDeviceInfo->nativeDataFormatCount ;
174+
175+ if (deviceInfo.nativeDataFormatCount > 0 ) {
176+ // Allocate memory for nativeDataFormats
177+ deviceInfo.nativeDataFormats = static_cast <native_data_format *>(ma_malloc (
178+ sizeof (native_data_format) * pCaptureDeviceInfo->nativeDataFormatCount ,
179+ nullptr ));
180+
181+ // Copy all nativeDataFormats
182+ for (ma_uint32 iFormat = 0 ; iFormat < pCaptureDeviceInfo->nativeDataFormatCount ; ++iFormat) {
183+ deviceInfo.nativeDataFormats [iFormat].format = pCaptureDeviceInfo->nativeDataFormats [iFormat].format ;
184+ deviceInfo.nativeDataFormats [iFormat].channels = pCaptureDeviceInfo->nativeDataFormats [iFormat].
185+ channels;
186+ deviceInfo.nativeDataFormats [iFormat].sampleRate = pCaptureDeviceInfo->nativeDataFormats [iFormat].
187+ sampleRate;
188+ deviceInfo.nativeDataFormats [iFormat].flags = pCaptureDeviceInfo->nativeDataFormats [iFormat].flags ;
189+ }
190+ }
191+
192+ if (captureDeviceInfos != nullptr )
193+ captureDeviceInfos[iDevice] = deviceInfo;
194+ }
195+
196+ *ppPlaybackDeviceInfos = playbackDeviceInfos;
197+ *ppCaptureDeviceInfos = captureDeviceInfos;
198+
199+ return result;
104200}
105201
106- // Seek current stream position to a specific time in seconds.
107- MA_API ma_result sf_decoder_seek_to_time (ma_decoder *decoder, const double timeInSec) {
108- if (timeInSec < 0 ) {
109- return MA_INVALID_ARGS;
202+
203+ /*
204+ MA_API ma_result sf_get_devices(ma_context *context, ma_device_info **ppPlaybackDeviceInfos,
205+ const ma_device_info **ppCaptureDeviceInfos, ma_uint32 *pPlaybackDeviceCount,
206+ ma_uint32 *pCaptureDeviceCount) {
207+ ma_device_info *pPlaybackDevices = nullptr;
208+ ma_device_info *pCaptureDevices = nullptr;
209+
210+ const auto result = ma_context_get_devices(context,
211+ &pPlaybackDevices,
212+ pPlaybackDeviceCount,
213+ &pCaptureDevices,
214+ pCaptureDeviceCount);
215+
216+ if (result == MA_SUCCESS) {
217+ *ppPlaybackDeviceInfos = pPlaybackDevices;
218+ *ppCaptureDeviceInfos = pCaptureDevices;
110219 }
111220
112- auto target_frame = static_cast <ma_uint64>(timeInSec * decoder->outputSampleRate );
113- return ma_decoder_seek_to_pcm_frame (decoder, target_frame);
221+ return result;
114222}
223+
224+ */
115225} // End of extern "C" block
0 commit comments