History

player: add audio render

lieff committed 8 years ago

Commit 53cb2a11 parent 29f6dca6 files · +133 −8Browse files

Unified · Split

6 files
  1. player/audio_sdl.c +94 −0
  2. player/audio_sdl.h +11 −0
  3. player/build.sh +1 −1
  4. player/decode.c +4 −1
  5. player/decode.h +10 −1
  6. player/player.cpp +13 −5
player/audio_sdl.c+94−0
@@ -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+}
player/audio_sdl.h+11−0
@@ -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
player/build.sh+1−1
@@ -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
player/decode.c+4−1
@@ -1,3 +1,4 @@
1+#include <stdio.h>
12 #include <string.h>
23 #include <errno.h>
34 #include <sys/mman.h>
@@ -26,8 +27,9 @@ static void add_audio(decoder *dec, short *buf, int bytes) · 22 unmodified lines
2627 if (dec->mp3_buf)
2728 free(dec->mp3_buf);
2829 dec->mp3_buf = (short *)buf_new;
29− dec->mp3_size += bytes;
3030 }
31+ memcpy((char*)dec->mp3_buf + dec->mp3_size, buf, bytes);
32+ dec->mp3_size += bytes;
3133 LeaveCriticalSection(&dec->mp3_lock);
3234 }
3335
@@ -98,6 +100,7 @@ int preload_mp3(decoder *dec, const char *file_name) · 64 unmodified lines
98100 free(dec->mp3_buf);
99101 dec->mp3_buf = 0;
100102 dec->mp3_size = 0;
103+ dec->mp3_pos = 0;
101104 dec->mp3_allocated = 0;
102105 dec->mp3_rate = 0;
103106 dec->mp3_channels = 0;
player/decode.h+10−1
@@ -1,14 +1,23 @@
11 #pragma once
22 #include "system.h"
3+#ifdef __cplusplus
4+extern "C" {
5+#endif
36
47 typedef struct decoder
58 {
69 CRITICAL_SECTION mp3_lock;
710 const char *file_name;
811 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;
1013 float mp3_duration;
1114 HANDLE mp3_open_thread;
1215 } decoder;
1316
17+extern decoder _dec;
18+
1419 int preload_mp3(decoder *dec, const char *file_name);
20+
21+#ifdef __cplusplus
22+}
23+#endif
player/player.cpp+13−5
@@ -10,6 +10,8 @@ · 9 unmodified lines
1010 #include <OpenGL/gl3.h>
1111 #endif
1212 #include "FreeSans.h"
13+#include "audio_sdl.h"
14+#include "decode.h"
1315
1416 #define STB_IMAGE_IMPLEMENTATION
1517 #include "stb_image.h"
@@ -32,10 +34,11 @@ · 16 unmodified lines
3234 #ifdef USE_GLES3
3335 static PFNGLGENERATEMIPMAPPROC glGenerateMipmap;
3436 #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;
3942
4043 static int load_image(const stbi_uc *data, int len)
4144 {
@@ -156,7 +159,7 @@ static void tick() · 114 unmodified lines
156159 if (nk_button_label(ctx, "Play") && _selected < (int)_playlist.size())
157160 {
158161 _play_state = 1;
159− //play();
162+ preload_mp3(&_dec, _playlist[_selected].c_str());
160163 }
161164 } else
162165 {
@@ -196,10 +199,15 @@ static void tick() · 33 unmodified lines
196199
197200 int main(int argc, char *argv[])
198201 {
202+ InitializeCriticalSection(&_dec.mp3_lock);
203+ sdl_audio_init(&_render, 44100, 2, 0, 0);
199204 init();
205+ for (int i = 1; i < argc; i++)
206+ _playlist.push_back(argv[i]);
200207 while (!glfwWindowShouldClose(_mainWindow))
201208 {
202209 tick();
203210 }
204211 close();
212+ sdl_audio_release(_render);
205213 }

Keyboard shortcuts

General

Show keyboard shortcuts
?
Focus the current search
/

Issues and pull requests

Open filters
f

Issues, pull requests and Inbox

Select next item
j↓
Select previous item
k↑
Open selected item
Enter
Dismiss filters or clear selection
Esc

Repository code

Go to file
t