Unified · Split
| @@ -0,0 +1,94 @@ | ||
| 1 | +#include <stddef.h> | |
| 2 | +#include <stdlib.h> | |
| 3 | +#include <sys/param.h> | |
| 4 | +#include <SDL2/SDL.h> | |
| 5 | +#include <assert.h> | |
| 6 | +#include <stdio.h> | |
| 7 | +#include <string.h> | |
| 8 | +#include "decode.h" | |
| 9 | + | |
| 10 | +#ifndef MIN | |
| 11 | +#define MIN(a,b) ((a) < (b) ? (a) : (b)) | |
| 12 | +#endif | |
| 13 | +#define SDL2 | |
| 14 | + | |
| 15 | +typedef struct audio_ctx | |
| 16 | +{ | |
| 17 | +#ifdef SDL2 | |
| 18 | + int dev; | |
| 19 | +#endif | |
| 20 | + SDL_AudioSpec outputSpec; | |
| 21 | +} audio_ctx; | |
| 22 | + | |
| 23 | +decoder _dec; | |
| 24 | + | |
| 25 | +static void audio_cb(void *udata, Uint8 *stream, int len) | |
| 26 | +{ | |
| 27 | + audio_ctx *ctx = (audio_ctx *)udata; | |
| 28 | + memset(stream, 0, len); | |
| 29 | + EnterCriticalSection(&_dec.mp3_lock); | |
| 30 | + if ((_dec.mp3_size - _dec.mp3_pos) >= len) | |
| 31 | + { | |
| 32 | + memcpy(stream, (char*)_dec.mp3_buf + _dec.mp3_pos, len); | |
| 33 | + _dec.mp3_pos += len; | |
| 34 | + } | |
| 35 | + LeaveCriticalSection(&_dec.mp3_lock); | |
| 36 | +} | |
| 37 | + | |
| 38 | +int sdl_audio_init(void **audio_render, int samplerate, int channels, int format, int buffer) | |
| 39 | +{ | |
| 40 | + *audio_render = 0; | |
| 41 | + audio_ctx *ctx = calloc(1, sizeof(audio_ctx)); | |
| 42 | + SDL_AudioSpec wanted; | |
| 43 | + memset(&wanted, 0, sizeof(wanted)); | |
| 44 | + wanted.freq = samplerate; | |
| 45 | + wanted.format = format ? AUDIO_F32 : AUDIO_S16; | |
| 46 | + wanted.channels = channels; | |
| 47 | + wanted.samples = buffer ? buffer : 4096; | |
| 48 | + wanted.callback = audio_cb; | |
| 49 | + wanted.userdata = ctx; | |
| 50 | + if (SDL_Init(SDL_INIT_AUDIO) < 0) | |
| 51 | + { | |
| 52 | + printf("error: sdl init failed: %s\n", SDL_GetError()); | |
| 53 | + return 0; | |
| 54 | + } | |
| 55 | +#ifdef SDL2 | |
| 56 | + int dev = SDL_OpenAudioDevice(NULL, 0, &wanted, &ctx->outputSpec, SDL_AUDIO_ALLOW_ANY_CHANGE); | |
| 57 | + if (dev <= 0) | |
| 58 | + { | |
| 59 | + printf("error: couldn't open audio: %s\n", SDL_GetError()); | |
| 60 | + free(ctx); | |
| 61 | + return 0; | |
| 62 | + } | |
| 63 | + ctx->dev = dev; | |
| 64 | +#else | |
| 65 | + if (SDL_OpenAudio(&wanted, &ctx->outputSpec) < 0) | |
| 66 | + { | |
| 67 | + printf("error: couldn't open audio: %s\n", SDL_GetError()); | |
| 68 | + return 0; | |
| 69 | + } | |
| 70 | +#endif | |
| 71 | +#ifdef SDL2 | |
| 72 | + SDL_PauseAudioDevice(dev, 0); | |
| 73 | +#else | |
| 74 | + SDL_PauseAudio(0); | |
| 75 | +#endif | |
| 76 | + *audio_render = ctx; | |
| 77 | + return 1; | |
| 78 | +} | |
| 79 | + | |
| 80 | +void sdl_audio_release(void *audio_render) | |
| 81 | +{ | |
| 82 | + audio_ctx *ctx = (audio_ctx *)audio_render; | |
| 83 | +#ifdef SDL2 | |
| 84 | + if (ctx->dev) | |
| 85 | + { | |
| 86 | + SDL_CloseAudioDevice(ctx->dev); | |
| 87 | + ctx->dev = 0; | |
| 88 | + } | |
| 89 | +#else | |
| 90 | + SDL_PauseAudio(1); | |
| 91 | + SDL_CloseAudio(); | |
| 92 | +#endif | |
| 93 | + free(ctx); | |
| 94 | +} | |
| @@ -0,0 +1,11 @@ | ||
| 1 | +#pragma once | |
| 2 | +#ifdef __cplusplus | |
| 3 | +extern "C" { | |
| 4 | +#endif | |
| 5 | + | |
| 6 | +int sdl_audio_init(void **audio_render, int samplerate, int channels, int format, int buffer); | |
| 7 | +void sdl_audio_release(void *audio_render); | |
| 8 | + | |
| 9 | +#ifdef __cplusplus | |
| 10 | +} | |
| 11 | +#endif | |
| @@ -1 +1 @@ | ||
| 1 | −gcc -O2 -o player *.cpp *.c -lstdc++ -lglfw -lGL -lpthread -lm -ldl | |
| 1 | +gcc -O2 -o player *.cpp *.c -lstdc++ -lglfw -lGL -lpthread -lm -ldl -lSDL2 | |
| @@ -1,3 +1,4 @@ | ||
| 1 | +#include <stdio.h> | |
| 1 | 2 | #include <string.h> |
| 2 | 3 | #include <errno.h> |
| 3 | 4 | #include <sys/mman.h> |
| @@ -26,8 +27,9 @@ static void add_audio(decoder *dec, short *buf, int bytes) · 22 unmodified lines | ||
| 26 | 27 | if (dec->mp3_buf) |
| 27 | 28 | free(dec->mp3_buf); |
| 28 | 29 | dec->mp3_buf = (short *)buf_new; |
| 29 | − dec->mp3_size += bytes; | |
| 30 | 30 | } |
| 31 | + memcpy((char*)dec->mp3_buf + dec->mp3_size, buf, bytes); | |
| 32 | + dec->mp3_size += bytes; | |
| 31 | 33 | LeaveCriticalSection(&dec->mp3_lock); |
| 32 | 34 | } |
| 33 | 35 | |
| @@ -98,6 +100,7 @@ int preload_mp3(decoder *dec, const char *file_name) · 64 unmodified lines | ||
| 98 | 100 | free(dec->mp3_buf); |
| 99 | 101 | dec->mp3_buf = 0; |
| 100 | 102 | dec->mp3_size = 0; |
| 103 | + dec->mp3_pos = 0; | |
| 101 | 104 | dec->mp3_allocated = 0; |
| 102 | 105 | dec->mp3_rate = 0; |
| 103 | 106 | dec->mp3_channels = 0; |
| @@ -1,14 +1,23 @@ | ||
| 1 | 1 | #pragma once |
| 2 | 2 | #include "system.h" |
| 3 | +#ifdef __cplusplus | |
| 4 | +extern "C" { | |
| 5 | +#endif | |
| 3 | 6 | |
| 4 | 7 | typedef struct decoder |
| 5 | 8 | { |
| 6 | 9 | CRITICAL_SECTION mp3_lock; |
| 7 | 10 | const char *file_name; |
| 8 | 11 | short *mp3_buf; |
| 9 | − int mp3_size, mp3_allocated, mp3_file_size, mp3_rate, mp3_channels; | |
| 12 | + int mp3_size, mp3_pos, mp3_allocated, mp3_file_size, mp3_rate, mp3_channels; | |
| 10 | 13 | float mp3_duration; |
| 11 | 14 | HANDLE mp3_open_thread; |
| 12 | 15 | } decoder; |
| 13 | 16 | |
| 17 | +extern decoder _dec; | |
| 18 | + | |
| 14 | 19 | int preload_mp3(decoder *dec, const char *file_name); |
| 20 | + | |
| 21 | +#ifdef __cplusplus | |
| 22 | +} | |
| 23 | +#endif | |
| @@ -10,6 +10,8 @@ · 9 unmodified lines | ||
| 10 | 10 | #include <OpenGL/gl3.h> |
| 11 | 11 | #endif |
| 12 | 12 | #include "FreeSans.h" |
| 13 | +#include "audio_sdl.h" | |
| 14 | +#include "decode.h" | |
| 13 | 15 | |
| 14 | 16 | #define STB_IMAGE_IMPLEMENTATION |
| 15 | 17 | #include "stb_image.h" |
| @@ -32,10 +34,11 @@ · 16 unmodified lines | ||
| 32 | 34 | #ifdef USE_GLES3 |
| 33 | 35 | static PFNGLGENERATEMIPMAPPROC glGenerateMipmap; |
| 34 | 36 | #endif |
| 35 | −void *_ctx; | |
| 36 | −GLFWwindow *_mainWindow; | |
| 37 | −std::map<std::string, int> _previews; | |
| 38 | −std::vector<std::string> _playlist; | |
| 37 | +static void *_ctx; | |
| 38 | +static GLFWwindow *_mainWindow; | |
| 39 | +static std::map<std::string, int> _previews; | |
| 40 | +static std::vector<std::string> _playlist; | |
| 41 | +static void *_render; | |
| 39 | 42 | |
| 40 | 43 | static int load_image(const stbi_uc *data, int len) |
| 41 | 44 | { |
| @@ -156,7 +159,7 @@ static void tick() · 114 unmodified lines | ||
| 156 | 159 | if (nk_button_label(ctx, "Play") && _selected < (int)_playlist.size()) |
| 157 | 160 | { |
| 158 | 161 | _play_state = 1; |
| 159 | − //play(); | |
| 162 | + preload_mp3(&_dec, _playlist[_selected].c_str()); | |
| 160 | 163 | } |
| 161 | 164 | } else |
| 162 | 165 | { |
| @@ -196,10 +199,15 @@ static void tick() · 33 unmodified lines | ||
| 196 | 199 | |
| 197 | 200 | int main(int argc, char *argv[]) |
| 198 | 201 | { |
| 202 | + InitializeCriticalSection(&_dec.mp3_lock); | |
| 203 | + sdl_audio_init(&_render, 44100, 2, 0, 0); | |
| 199 | 204 | init(); |
| 205 | + for (int i = 1; i < argc; i++) | |
| 206 | + _playlist.push_back(argv[i]); | |
| 200 | 207 | while (!glfwWindowShouldClose(_mainWindow)) |
| 201 | 208 | { |
| 202 | 209 | tick(); |
| 203 | 210 | } |
| 204 | 211 | close(); |
| 212 | + sdl_audio_release(_render); | |
| 205 | 213 | } |