4 This file is part of PulseAudio.
6 PulseAudio is free software; you can redistribute it and/or modify
7 it under the terms of the GNU Lesser General Public License as published
8 by the Free Software Foundation; either version 2 of the License,
9 or (at your option) any later version.
11 PulseAudio is distributed in the hope that it will be useful, but
12 WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 General Public License for more details.
16 You should have received a copy of the GNU Lesser General Public License
17 along with PulseAudio; if not, write to the Free Software
18 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
33 #include <pulse/xmalloc.h>
35 #include <pulsecore/sink-input.h>
36 #include <pulsecore/log.h>
38 #include "sound-file-stream.h"
40 #define BUF_SIZE (1024*10)
44 pa_sink_input *sink_input;
46 sf_count_t (*readf_function)(SNDFILE *sndfile, void *ptr, sf_count_t frames);
49 static void free_userdata(struct userdata *u) {
52 pa_sink_input_disconnect(u->sink_input);
53 pa_sink_input_unref(u->sink_input);
56 if (u->memchunk.memblock)
57 pa_memblock_unref(u->memchunk.memblock);
64 static void sink_input_kill(pa_sink_input *i) {
65 assert(i && i->userdata);
66 free_userdata(i->userdata);
69 static int sink_input_peek(pa_sink_input *i, pa_memchunk *chunk) {
71 assert(i && chunk && i->userdata);
74 if (!u->memchunk.memblock) {
75 uint32_t fs = pa_frame_size(&i->sample_spec);
78 u->memchunk.memblock = pa_memblock_new(i->sink->core->mempool, BUF_SIZE);
79 u->memchunk.index = 0;
81 if (u->readf_function) {
82 if ((n = u->readf_function(u->sndfile, u->memchunk.memblock->data, BUF_SIZE/fs)) <= 0)
85 u->memchunk.length = n * fs;
87 if ((n = sf_read_raw(u->sndfile, u->memchunk.memblock->data, BUF_SIZE)) <= 0)
90 u->memchunk.length = n;
93 if (!u->memchunk.length) {
100 pa_memblock_ref(chunk->memblock);
101 assert(chunk->length);
105 static void sink_input_drop(pa_sink_input *i, const pa_memchunk*chunk, size_t length) {
107 assert(i && chunk && length && i->userdata);
110 assert(!memcmp(chunk, &u->memchunk, sizeof(chunk)));
111 assert(length <= u->memchunk.length);
113 u->memchunk.index += length;
114 u->memchunk.length -= length;
116 if (u->memchunk.length <= 0) {
117 pa_memblock_unref(u->memchunk.memblock);
118 u->memchunk.memblock = NULL;
119 u->memchunk.index = u->memchunk.length = 0;
126 const pa_cvolume *volume) {
128 struct userdata *u = NULL;
131 pa_sink_input_new_data data;
136 u = pa_xnew(struct userdata, 1);
137 u->sink_input = NULL;
138 u->memchunk.memblock = NULL;
139 u->memchunk.index = u->memchunk.length = 0;
142 memset(&sfinfo, 0, sizeof(sfinfo));
144 if (!(u->sndfile = sf_open(fname, SFM_READ, &sfinfo))) {
145 pa_log("Failed to open file %s", fname);
149 u->readf_function = NULL;
151 switch (sfinfo.format & 0xFF) {
152 case SF_FORMAT_PCM_16:
153 case SF_FORMAT_PCM_U8:
154 case SF_FORMAT_PCM_S8:
155 ss.format = PA_SAMPLE_S16NE;
156 u->readf_function = (sf_count_t (*)(SNDFILE *sndfile, void *ptr, sf_count_t frames)) sf_readf_short;
160 ss.format = PA_SAMPLE_ULAW;
164 ss.format = PA_SAMPLE_ALAW;
167 case SF_FORMAT_FLOAT:
169 ss.format = PA_SAMPLE_FLOAT32NE;
170 u->readf_function = (sf_count_t (*)(SNDFILE *sndfile, void *ptr, sf_count_t frames)) sf_readf_float;
174 ss.rate = sfinfo.samplerate;
175 ss.channels = sfinfo.channels;
177 if (!pa_sample_spec_valid(&ss)) {
178 pa_log("Unsupported sample format in file %s", fname);
182 pa_sink_input_new_data_init(&data);
184 data.driver = __FILE__;
186 pa_sink_input_new_data_set_sample_spec(&data, &ss);
187 pa_sink_input_new_data_set_volume(&data, volume);
189 if (!(u->sink_input = pa_sink_input_new(sink->core, &data, 0)))
192 u->sink_input->peek = sink_input_peek;
193 u->sink_input->drop = sink_input_drop;
194 u->sink_input->kill = sink_input_kill;
195 u->sink_input->userdata = u;
197 pa_sink_notify(u->sink_input->sink);