| 1 | /* |
| 2 | Demonstrates interop between the high-level and the low-level API. |
| 3 | |
| 4 | In this example we are using `ma_device` (the low-level API) to capture data from the microphone |
| 5 | which we then play back through the engine as a sound. We use a ring buffer to act as the data |
| 6 | source for the sound. |
| 7 | |
| 8 | This is just a very basic example to show the general idea on how this might be achieved. In |
| 9 | this example a ring buffer is being used as the intermediary data source, but you can use anything |
| 10 | that works best for your situation. So long as the data is captured from the microphone, and then |
| 11 | delivered to the sound (via a data source), you should be good to go. |
| 12 | |
| 13 | A more robust example would probably not want to use a ring buffer directly as the data source. |
| 14 | Instead you would probably want to do a custom data source that handles underruns and overruns of |
| 15 | the ring buffer and deals with desyncs between capture and playback. In the future this example |
| 16 | may be updated to make use of a more advanced data source that handles all of this. |
| 17 | */ |
| 18 | #include "../miniaudio.c" |
| 19 | |
| 20 | static ma_pcm_rb rb; |
| 21 | static ma_device device; |
| 22 | static ma_engine engine; |
| 23 | static ma_sound sound; /* The sound will be the playback of the capture side. */ |
| 24 | |
| 25 | void capture_data_callback(ma_device* pDevice, void* pFramesOut, const void* pFramesIn, ma_uint32 frameCount) |
| 26 | { |
| 27 | ma_result result; |
| 28 | ma_uint32 framesWritten; |
| 29 | |
| 30 | (void)pFramesOut; |
| 31 | |
| 32 | /* We need to write to the ring buffer. Need to do this in a loop. */ |
| 33 | framesWritten = 0; |
| 34 | while (framesWritten < frameCount) { |
| 35 | void* pMappedBuffer; |
| 36 | ma_uint32 framesToWrite = frameCount - framesWritten; |
| 37 | |
| 38 | result = ma_pcm_rb_acquire_write(&rb, &framesToWrite, &pMappedBuffer); |
| 39 | if (result != MA_SUCCESS) { |
| 40 | break; |
| 41 | } |
| 42 | |
| 43 | if (framesToWrite == 0) { |
| 44 | break; |
| 45 | } |
| 46 | |
| 47 | /* Copy the data from the capture buffer to the ring buffer. */ |
| 48 | ma_copy_pcm_frames(pMappedBuffer, ma_offset_pcm_frames_const_ptr_f32((const float*)pFramesIn, framesWritten, pDevice->capture.channels), framesToWrite, pDevice->capture.format, pDevice->capture.channels); |
| 49 | |
| 50 | result = ma_pcm_rb_commit_write(&rb, framesToWrite); |
| 51 | if (result != MA_SUCCESS) { |
| 52 | break; |
| 53 | } |
| 54 | |
| 55 | framesWritten += framesToWrite; |
| 56 | } |
| 57 | } |
| 58 | |
| 59 | int main(int argc, char** argv) |
| 60 | { |
| 61 | ma_result result; |
| 62 | ma_device_config deviceConfig; |
| 63 | |
| 64 | /* |
| 65 | The first thing we'll do is set up the capture side. There are two parts to this. The first is |
| 66 | the device itself, and the other is the ring buffer. It doesn't matter what order we initialize |
| 67 | these in, so long as the ring buffer is created before the device is started so that the |
| 68 | callback can be guaranteed to have a valid destination. We'll initialize the device first, and |
| 69 | then use the format, channels and sample rate to initialize the ring buffer. |
| 70 | |
| 71 | It's important that the sample format of the device is set to f32 because that's what the engine |
| 72 | uses internally. |
| 73 | */ |
| 74 | |
| 75 | /* Initialize the capture device. */ |
| 76 | deviceConfig = ma_device_config_init(ma_device_type_capture); |
| 77 | deviceConfig.capture.format = ma_format_f32; |
| 78 | deviceConfig.dataCallback = capture_data_callback; |
| 79 | |
| 80 | result = ma_device_init(NULL, &deviceConfig, &device); |
| 81 | if (result != MA_SUCCESS) { |
| 82 | printf("Failed to initialize capture device."); |
| 83 | return -1; |
| 84 | } |
| 85 | |
| 86 | /* Initialize the ring buffer. */ |
| 87 | result = ma_pcm_rb_init(device.capture.format, device.capture.channels, device.capture.internalPeriodSizeInFrames * 5, NULL, NULL, &rb); |
| 88 | if (result != MA_SUCCESS) { |
| 89 | printf("Failed to initialize the ring buffer."); |
| 90 | return -1; |
| 91 | } |
| 92 | |
| 93 | /* |
| 94 | Ring buffers don't require a sample rate for their normal operation, but we can associate it |
| 95 | with a sample rate. We'll want to do this so the engine can resample if necessary. |
| 96 | */ |
| 97 | ma_pcm_rb_set_sample_rate(&rb, device.sampleRate); |
| 98 | |
| 99 | |
| 100 | |
| 101 | /* |
| 102 | At this point the capture side is set up and we can now set up the playback side. Here we are |
| 103 | using `ma_engine` and linking the captured data to a sound so it can be manipulated just like |
| 104 | any other sound in the world. |
| 105 | |
| 106 | Note that we have not yet started the capture device. Since the captured data is tied to a |
| 107 | sound, we'll link the starting and stopping of the capture device to the starting and stopping |
| 108 | of the sound. |
| 109 | */ |
| 110 | |
| 111 | /* We'll get the engine up and running before we start the capture device. */ |
| 112 | result = ma_engine_init(NULL, &engine); |
| 113 | if (result != MA_SUCCESS) { |
| 114 | printf("Failed to initialize the engine."); |
| 115 | return -1; |
| 116 | } |
| 117 | |
| 118 | /* |
| 119 | We can now create our sound. This is created from a data source, which in this example is a |
| 120 | ring buffer. The capture side will be writing data into the ring buffer, whereas the sound |
| 121 | will be reading from it. |
| 122 | */ |
| 123 | result = ma_sound_init_from_data_source(&engine, &rb, 0, NULL, &sound); |
| 124 | if (result != MA_SUCCESS) { |
| 125 | printf("Failed to initialize the sound."); |
| 126 | return -1; |
| 127 | } |
| 128 | |
| 129 | /* Link the starting of the device and sound together. */ |
| 130 | ma_device_start(&device); |
| 131 | ma_sound_start(&sound); |
| 132 | |
| 133 | |
| 134 | printf("Press Enter to quit...\n"); |
| 135 | getchar(); |
| 136 | |
| 137 | ma_sound_uninit(&sound); |
| 138 | ma_engine_uninit(&engine); |
| 139 | ma_device_uninit(&device); |
| 140 | ma_pcm_rb_uninit(&rb); |
| 141 | |
| 142 | |
| 143 | (void)argc; |
| 144 | (void)argv; |
| 145 | return 0; |
| 146 | } |