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
15typedef struct audio_ctx
16{
17#ifdef SDL2
18 int dev;
19#endif
20 SDL_AudioSpec outputSpec;
21} audio_ctx;
22
23decoder _dec;
24
25static 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
38int 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
80void 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
3extern "C" {
4#endif
5
6int sdl_audio_init(void **audio_render, int samplerate, int channels, int format, int buffer);
7void sdl_audio_release(void *audio_render);
8
9#ifdef __cplusplus
10}
11#endif
player/build.sh+1−1
@@ -1 +1 @@
1gcc -O2 -o player *.cpp *.c -lstdc++ -lglfw -lGL -lpthread -lm -ldl1gcc -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>
1#include <string.h>2#include <string.h>
2#include <errno.h>3#include <errno.h>
3#include <sys/mman.h>4#include <sys/mman.h>
@@ -26,8 +27,9 @@ static void add_audio(decoder *dec, short *buf, int bytes) · 22 unmodified lines
26 if (dec->mp3_buf)27 if (dec->mp3_buf)
27 free(dec->mp3_buf);28 free(dec->mp3_buf);
28 dec->mp3_buf = (short *)buf_new;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 LeaveCriticalSection(&dec->mp3_lock);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 free(dec->mp3_buf);100 free(dec->mp3_buf);
99 dec->mp3_buf = 0;101 dec->mp3_buf = 0;
100 dec->mp3_size = 0;102 dec->mp3_size = 0;
103 dec->mp3_pos = 0;
101 dec->mp3_allocated = 0;104 dec->mp3_allocated = 0;
102 dec->mp3_rate = 0;105 dec->mp3_rate = 0;
103 dec->mp3_channels = 0;106 dec->mp3_channels = 0;
player/decode.h+10−1
@@ -1,14 +1,23 @@
1#pragma once1#pragma once
2#include "system.h"2#include "system.h"
3#ifdef __cplusplus
4extern "C" {
5#endif
3 6
4typedef struct decoder7typedef struct decoder
5{8{
6 CRITICAL_SECTION mp3_lock;9 CRITICAL_SECTION mp3_lock;
7 const char *file_name;10 const char *file_name;
8 short *mp3_buf;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 float mp3_duration;13 float mp3_duration;
11 HANDLE mp3_open_thread;14 HANDLE mp3_open_thread;
12} decoder;15} decoder;
13 16
17extern decoder _dec;
18
14int preload_mp3(decoder *dec, const char *file_name);19int 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
10#include <OpenGL/gl3.h>10#include <OpenGL/gl3.h>
11#endif11#endif
12#include "FreeSans.h"12#include "FreeSans.h"
13#include "audio_sdl.h"
14#include "decode.h"
13 15
14#define STB_IMAGE_IMPLEMENTATION16#define STB_IMAGE_IMPLEMENTATION
15#include "stb_image.h"17#include "stb_image.h"
@@ -32,10 +34,11 @@ · 16 unmodified lines
32#ifdef USE_GLES334#ifdef USE_GLES3
33static PFNGLGENERATEMIPMAPPROC glGenerateMipmap;35static PFNGLGENERATEMIPMAPPROC glGenerateMipmap;
34#endif36#endif
35void *_ctx;37static void *_ctx;
36GLFWwindow *_mainWindow;38static GLFWwindow *_mainWindow;
37std::map<std::string, int> _previews;39static std::map<std::string, int> _previews;
38std::vector<std::string> _playlist;40static std::vector<std::string> _playlist;
41static void *_render;
39 42
40static int load_image(const stbi_uc *data, int len)43static int load_image(const stbi_uc *data, int len)
41{44{
@@ -156,7 +159,7 @@ static void tick() · 114 unmodified lines
156 if (nk_button_label(ctx, "Play") && _selected < (int)_playlist.size())159 if (nk_button_label(ctx, "Play") && _selected < (int)_playlist.size())
157 {160 {
158 _play_state = 1;161 _play_state = 1;
159 //play();162 preload_mp3(&_dec, _playlist[_selected].c_str());
160 }163 }
161 } else164 } else
162 {165 {
@@ -196,10 +199,15 @@ static void tick() · 33 unmodified lines
196 199
197int main(int argc, char *argv[])200int main(int argc, char *argv[])
198{201{
202 InitializeCriticalSection(&_dec.mp3_lock);
203 sdl_audio_init(&_render, 44100, 2, 0, 0);
199 init();204 init();
205 for (int i = 1; i < argc; i++)
206 _playlist.push_back(argv[i]);
200 while (!glfwWindowShouldClose(_mainWindow))207 while (!glfwWindowShouldClose(_mainWindow))
201 {208 {
202 tick();209 tick();
203 }210 }
204 close();211 close();
212 sdl_audio_release(_render);
205}213}

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