Debian Codegraph @

examples/simple_playback_sine.c

raw symbols SCIP
1 /*
2 Demonstrates playback of a sine wave.
3
4 Since all this example is doing is playing back a sine wave, we can disable decoding (and encoding) which will slightly
5 reduce the size of the executable. This is done with the `MA_NO_DECODING` and `MA_NO_ENCODING` options.
6
7 The generation of sine wave is achieved via the `ma_waveform` API. A waveform is a data source which means it can be
8 seamlessly plugged into the `ma_data_source_*()` family of APIs as well.
9
10 A waveform is initialized using the standard config/init pattern used throughout all of miniaudio. Frames are read via
11 the `ma_waveform_read_pcm_frames()` API.
12
13 This example works with Emscripten.
14 */
15 #define MA_NO_DECODING
16 #define MA_NO_ENCODING
17 #include "../miniaudio.c"
18
19 #include <stdio.h>
20
21 #ifdef __EMSCRIPTEN__
22 #include <emscripten.h>
23
24 void main_loop__em()
25 {
26 }
27 #endif
28
29 #define DEVICE_FORMAT ma_format_f32
30 #define DEVICE_CHANNELS 2
31 #define DEVICE_SAMPLE_RATE 48000
32
33 void data_callback(ma_device* pDevice, void* pOutput, const void* pInput, ma_uint32 frameCount)
34 {
35 ma_waveform_read_pcm_frames((ma_waveform*)pDevice->pUserData, pOutput, frameCount, NULL);
36
37 (void)pInput; /* Unused. */
38 }
39
40 int main(int argc, char** argv)
41 {
42 ma_waveform sineWave;
43 ma_device_config deviceConfig;
44 ma_device device;
45 ma_waveform_config sineWaveConfig;
46
47 deviceConfig = ma_device_config_init(ma_device_type_playback);
48 deviceConfig.playback.format = DEVICE_FORMAT;
49 deviceConfig.playback.channels = DEVICE_CHANNELS;
50 deviceConfig.sampleRate = DEVICE_SAMPLE_RATE;
51 deviceConfig.dataCallback = data_callback;
52 deviceConfig.pUserData = &sineWave;
53
54 if (ma_device_init(NULL, &deviceConfig, &device) != MA_SUCCESS) {
55 printf("Failed to open playback device.\n");
56 return -4;
57 }
58
59 printf("Device Name: %s\n", device.playback.name);
60
61 sineWaveConfig = ma_waveform_config_init(device.playback.format, device.playback.channels, device.sampleRate, ma_waveform_type_sine, 0.2, 220);
62 ma_waveform_init(&sineWaveConfig, &sineWave);
63
64 if (ma_device_start(&device) != MA_SUCCESS) {
65 printf("Failed to start playback device.\n");
66 ma_device_uninit(&device);
67 return -5;
68 }
69
70 #ifdef __EMSCRIPTEN__
71 emscripten_set_main_loop(main_loop__em, 0, 1);
72 #else
73 printf("Press Enter to quit...\n");
74 getchar();
75 #endif
76
77 ma_device_uninit(&device);
78 ma_waveform_uninit(&sineWave); /* Uninitialize the waveform after the device so we don't pull it from under the device while it's being reference in the data callback. */
79
80 (void)argc;
81 (void)argv;
82 return 0;
83 }