1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
|
#include <SDL/SDL.h>
#include "psdl.h"
#include "streamer.h"
#include "common.h"
static int sdl_player_numsamples = 4096;
static int sdl_player_freq;
static SDL_AudioSpec spec;
static void psdl_callback (void *userdata, Uint8 *stream, int len);
static float sdl_volume = 1;
static inline void
le_int16 (int16_t in, char *out) {
char *pin = (char *)∈
#if !BIGENDIAN
out[0] = pin[0];
out[1] = pin[1];
#else
out[1] = pin[0];
out[0] = pin[1];
#endif
}
int
psdl_init (void) {
SDL_AudioSpec obt;
int formats[] = { AUDIO_S16, -1 };
int freqs[] = { 48000, 44100, 96000, 22050, -1 };
const char *fmtnames[] = { "16 bit signed integer" };
int fmt, frq;
int success = 0;
fprintf (stderr, "sdl_player_init\n");
for (fmt = 0; formats[fmt] != -1; fmt++) {
for (frq = 0; freqs[frq] != -1; frq++) {
fprintf(stderr, "SDL: trying %s @ %dHz\n", fmtnames[fmt], freqs[frq]);
spec.freq = freqs[frq];
spec.format = formats[fmt];
spec.channels = 2;
spec.samples = sdl_player_numsamples;
spec.callback = psdl_callback;
if (SDL_OpenAudio(&spec, &obt) < 0) {
fprintf(stderr, "SDL: couldn't open audio: %s\n", SDL_GetError());
}
else {
success = 1;
break;
}
}
}
if (!success) {
return -1;
}
sdl_player_numsamples = obt.samples; //numsamples;
sdl_player_freq = obt.freq;
fprintf (stderr, "SDL: got %d frame size (requested %d), %dHz\n", obt.samples, sdl_player_numsamples, sdl_player_freq);
return 0;
}
void
psdl_free (void) {
SDL_CloseAudio ();
}
int
psdl_play (void) {
SDL_PauseAudio (0);
return 0;
}
int
psdl_stop (void) {
SDL_PauseAudio (1);
}
int
psdl_ispaused (void) {
return (SDL_GetAudioStatus () == SDL_AUDIO_PAUSED);
}
int
psdl_pause (void) {
SDL_PauseAudio (1);
return 0;
}
int
psdl_unpause (void) {
SDL_PauseAudio (0);
return 0;
}
void
psdl_set_volume (float vol) {
sdl_volume = vol;
}
int
psdl_get_rate (void) {
return sdl_player_freq;
}
static void
psdl_callback (void* userdata, Uint8 *stream, int len) {
int bytesread = streamer_read (stream, len);
int ivolume = sdl_volume * 1000;
for (int i = 0; i < bytesread/2; i++) {
int16_t sample = (int16_t)(((int32_t)(((int16_t*)stream)[i])) * ivolume / 1000);
le_int16 (sample, (char*)&(((int16_t*)stream)[i]));
}
if (bytesread < len) {
memset (stream + bytesread, 0, len-bytesread);
}
}
|