@@ -435,26 +435,43 @@ bool lovrAudioSetDevice(AudioType type, void* id, size_t size, bool read, AudioS
435435 ma_data_converter_uninit (& state .playbackConverter , NULL );
436436 result = ma_data_converter_init (& converterConfig , NULL , & state .playbackConverter );
437437 lovrAssert (result == MA_SUCCESS , "Failed to create sink data converter: %s" , ma_result_description (result ));
438+ lovrRetain (stream );
438439 } else {
439- stream = lovrAudioStreamCreate (state .config .sampleRate * 1. , SAMPLE_F32 , 2 , state .config .sampleRate );
440+ stream = lovrAudioStreamCreate (state .config .sampleRate , SAMPLE_F32 , 2 , state .config .sampleRate );
440441 }
441442 }
442443 } else {
443- Sound * sound = lovrAudioStreamGetSound (stream );
444444 config = ma_device_config_init (ma_device_type_capture );
445445 config .capture .pDeviceID = (ma_device_id * ) id ;
446446 config .capture .shareMode = shareModes [shareMode ];
447- config .capture .format = miniaudioFormats [lovrSoundGetFormat (sound )];
448- config .capture .channels = lovrSoundGetChannelCount (sound );
449- config .sampleRate = lovrSoundGetSampleRate (sound );
450447 config .periodSizeInFrames = BUFFER_SIZE ;
451448 config .dataCallback = onCapture ;
449+
450+ if (stream ) {
451+ Sound * sound = lovrAudioStreamGetSound (stream );
452+ config .capture .format = miniaudioFormats [lovrSoundGetFormat (sound )];
453+ config .capture .channels = lovrSoundGetChannelCount (sound );
454+ config .sampleRate = lovrSoundGetSampleRate (sound );
455+ lovrRetain (stream );
456+ } else {
457+ config .capture .format = ma_format_f32 ;
458+ config .capture .channels = 1 ;
459+ }
452460 }
453461
454462 result = ma_device_init (& state .context , & config , & state .devices [type ]);
455- lovrAssert (result == MA_SUCCESS , "Failed to initialize device: %s" , ma_result_description (result ));
463+
464+ if (result != MA_SUCCESS ) {
465+ lovrRelease (stream , lovrAudioStreamDestroy );
466+ return lovrSetError ("Failed to initialize device: %s" , ma_result_description (result ));
467+ }
468+
469+ if (type == AUDIO_CAPTURE && !stream ) {
470+ uint32_t sampleRate = state .devices [AUDIO_CAPTURE ].sampleRate ;
471+ stream = lovrAudioStreamCreate (sampleRate , SAMPLE_F32 , 1 , sampleRate );
472+ }
473+
456474 state .streams [type ] = stream ;
457- lovrRetain (stream );
458475 return true;
459476}
460477
@@ -463,6 +480,10 @@ AudioStream* lovrAudioGetStream(AudioType type) {
463480}
464481
465482bool lovrAudioStart (AudioType type ) {
483+ if (!state .devices [type ].pContext ) {
484+ return lovrSetError ("no device" );
485+ }
486+
466487 ma_result result = ma_device_start (& state .devices [type ]);
467488 lovrAssert (result == MA_SUCCESS , ma_result_description (result ));
468489}
0 commit comments