| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
#define MA_NO_DEVICE_IO |
| 12 |
#include "../miniaudio.c" |
| 13 |
|
| 14 |
#define SDL_MAIN_HANDLED |
| 15 |
#include <SDL2/SDL.h> |
| 16 |
|
| 17 |
#define CHANNELS 2 |
| 18 |
#define SAMPLE_RATE 48000 |
| 19 |
|
| 20 |
static ma_engine g_engine; |
| 21 |
static ma_sound g_sound; |
| 22 |
|
| 23 |
void data_callback(void* pUserData, ma_uint8* pBuffer, int bufferSizeInBytes) |
| 24 |
{ |
| 25 |
ma_uint32 bufferSizeInFrames; |
| 26 |
|
| 27 |
(void)pUserData; |
| 28 |
|
| 29 |
|
| 30 |
bufferSizeInFrames = (ma_uint32)bufferSizeInBytes / ma_get_bytes_per_frame(ma_format_f32, ma_engine_get_channels(&g_engine)); |
| 31 |
ma_engine_read_pcm_frames(&g_engine, pBuffer, bufferSizeInFrames, NULL); |
| 32 |
} |
| 33 |
|
| 34 |
int main(int argc, char** argv) |
| 35 |
{ |
| 36 |
ma_result result; |
| 37 |
ma_engine_config engineConfig; |
| 38 |
SDL_AudioSpec desiredSpec; |
| 39 |
SDL_AudioSpec obtainedSpec; |
| 40 |
SDL_AudioDeviceID deviceID; |
| 41 |
|
| 42 |
if (argc < 2) { |
| 43 |
printf("No input file."); |
| 44 |
return -1; |
| 45 |
} |
| 46 |
|
| 47 |
|
| 48 |
|
| 49 |
|
| 50 |
|
| 51 |
|
| 52 |
|
| 53 |
|
| 54 |
|
| 55 |
engineConfig = ma_engine_config_init(); |
| 56 |
engineConfig.noDevice = MA_TRUE; |
| 57 |
engineConfig.channels = CHANNELS; |
| 58 |
engineConfig.sampleRate = SAMPLE_RATE; |
| 59 |
|
| 60 |
result = ma_engine_init(&engineConfig, &g_engine); |
| 61 |
if (result != MA_SUCCESS) { |
| 62 |
printf("Failed to initialize audio engine."); |
| 63 |
return -1; |
| 64 |
} |
| 65 |
|
| 66 |
|
| 67 |
result = ma_sound_init_from_file(&g_engine, argv[1], 0, NULL, NULL, &g_sound); |
| 68 |
if (result != MA_SUCCESS) { |
| 69 |
printf("Failed to initialize sound."); |
| 70 |
return -1; |
| 71 |
} |
| 72 |
|
| 73 |
|
| 74 |
ma_sound_set_looping(&g_sound, MA_TRUE); |
| 75 |
|
| 76 |
|
| 77 |
|
| 78 |
|
| 79 |
|
| 80 |
ma_sound_start(&g_sound); |
| 81 |
|
| 82 |
|
| 83 |
|
| 84 |
|
| 85 |
|
| 86 |
|
| 87 |
if (SDL_InitSubSystem(SDL_INIT_AUDIO) != 0) { |
| 88 |
printf("Failed to initialize SDL sub-system."); |
| 89 |
return -1; |
| 90 |
} |
| 91 |
|
| 92 |
MA_ZERO_OBJECT(&desiredSpec); |
| 93 |
desiredSpec.freq = ma_engine_get_sample_rate(&g_engine); |
| 94 |
desiredSpec.format = AUDIO_F32; |
| 95 |
desiredSpec.channels = ma_engine_get_channels(&g_engine); |
| 96 |
desiredSpec.samples = 512; |
| 97 |
desiredSpec.callback = data_callback; |
| 98 |
desiredSpec.userdata = NULL; |
| 99 |
|
| 100 |
deviceID = SDL_OpenAudioDevice(NULL, 0, &desiredSpec, &obtainedSpec, SDL_AUDIO_ALLOW_ANY_CHANGE); |
| 101 |
if (deviceID == 0) { |
| 102 |
printf("Failed to open SDL audio device."); |
| 103 |
return -1; |
| 104 |
} |
| 105 |
|
| 106 |
|
| 107 |
SDL_PauseAudioDevice(deviceID, 0); |
| 108 |
|
| 109 |
#if 1 |
| 110 |
{ |
| 111 |
|
| 112 |
float stepAngle = 0.002f; |
| 113 |
float angle = 0; |
| 114 |
float distance = 2; |
| 115 |
|
| 116 |
for (;;) { |
| 117 |
double x = ma_cosd(angle) - ma_sind(angle); |
| 118 |
double y = ma_sind(angle) + ma_cosd(angle); |
| 119 |
|
| 120 |
ma_sound_set_position(&g_sound, (float)x * distance, 0, (float)y * distance); |
| 121 |
|
| 122 |
angle += stepAngle; |
| 123 |
ma_sleep(1); |
| 124 |
} |
| 125 |
} |
| 126 |
#else |
| 127 |
printf("Press Enter to quit..."); |
| 128 |
getchar(); |
| 129 |
#endif |
| 130 |
|
| 131 |
ma_sound_uninit(&g_sound); |
| 132 |
ma_engine_uninit(&g_engine); |
| 133 |
SDL_CloseAudioDevice(deviceID); |
| 134 |
SDL_QuitSubSystem(SDL_INIT_AUDIO); |
| 135 |
|
| 136 |
return 0; |
| 137 |
} |