summaryrefslogtreecommitdiff
path: root/psdl.c
blob: 3e47837af08abdf016c998dcda7a9cf56d6102e0 (plain)
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
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
#include <SDL/SDL.h>
#include <samplerate.h>
#include "psdl.h"
#include "codec.h"
#include "playlist.h"

static int sdl_player_numsamples = 2<<25;
static int sdl_player_freq;
static char *sdl_buffer;
static float *sdl_fbuffer;
static float *sdl_srcbuffer;
static SDL_AudioSpec spec;
static void psdl_callback (void *userdata, Uint8 *stream, int len);
static codec_t *codec;

static SRC_STATE *src;
static SRC_DATA srcdata;
static int codecleft;

static float sdl_volume = 1;

int
psdl_init (void) {
	SDL_AudioSpec obt;
	int formats[] = { AUDIO_S16, -1 };
	int freqs[] = { 48000, 44100, -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;
    // source samplerate may be up to 96KHz, so need bigger buffers
	sdl_buffer = malloc (sdl_player_numsamples * sizeof (uint16_t) * 2 * 2);
	sdl_fbuffer = malloc (sdl_player_numsamples * sizeof (float) * 2 * 2); 
	sdl_srcbuffer = malloc (sdl_player_numsamples * sizeof (float) * 2);
	sdl_player_freq = obt.freq;
	fprintf (stderr, "SDL: got %d frame size (requested %d), %dHz\n", obt.samples, sdl_player_numsamples, sdl_player_freq);

//    src = src_new (SRC_SINC_BEST_QUALITY, 2, NULL);
    src = src_new (SRC_LINEAR, 2, NULL);
    codecleft = 0;

	return 0;
}

void
psdl_free (void) {
	SDL_CloseAudio ();
	if (sdl_buffer) {
        free (sdl_buffer);
        sdl_buffer = NULL;
    }
    src_delete (src);
    src = NULL;
}

int
psdl_play (struct playItem_s *it) {
    printf ("psdl_play\n");
    if (!it) {
        return -1;
    }
    codec = it->codec;
    if (codec->init (it->fname)) {
        return -1;
    }
	SDL_PauseAudio (0);
	return 0;
}

int
psdl_stop (void) {
	SDL_PauseAudio (1);
    if (codec) {
        codec->free ();
        codec = NULL;
    }
}

int
psdl_pause (void) {
	SDL_PauseAudio (1);
}

void
psdl_set_volume (float vol) {
    sdl_volume = vol;
}

static void
psdl_callback (void* userdata, Uint8 *stream, int len) {
    if (!codec) {
        memset (stream, 0, len);
    }
    else if (codec->info.samplesPerSecond == sdl_player_freq) {
        codec->read (sdl_buffer, len);
    }
    else {
        int nsamples = len/4;
        // convert to codec samplerate
        // add 5 extra samples for SRC
        nsamples = nsamples * codec->info.samplesPerSecond / sdl_player_freq + 5;
        // read data at source samplerate (with some room for SRC)
        codec->read (sdl_buffer, (nsamples - codecleft) * 4);
        // convert to float, and apply soft volume
        int i;
        float *fbuffer = sdl_fbuffer + codecleft*2;
        for (i = 0; i < (nsamples - codecleft) * 2; i++) {
            fbuffer[i] = ((int16_t *)sdl_buffer)[i]/32767.f;
            sdl_fbuffer[i+codecleft*2] *= sdl_volume;
        }
        // convert samplerate
        srcdata.data_in = sdl_fbuffer;
        srcdata.data_out = sdl_srcbuffer;
        srcdata.input_frames = nsamples;
        srcdata.output_frames = len/4;
        srcdata.src_ratio = (double)sdl_player_freq/codec->info.samplesPerSecond;
        srcdata.end_of_input = 0;
        src_process (src, &srcdata);
        // convert back to s16 format
        for (i = 0; i < len/2; i++) {
            ((int16_t*)stream)[i] = (int16_t)(sdl_srcbuffer[i]*32767.f);
        }
        // calculate how many unused input samples left
        codecleft = nsamples - srcdata.input_frames_used;
        // copy spare samples for next update
        memmove (sdl_fbuffer, &sdl_fbuffer[srcdata.input_frames_used*2], codecleft * 8);
    }
}