4 This file is part of PulseAudio.
6 Copyright 2004-2006 Lennart Poettering
8 PulseAudio is free software; you can redistribute it and/or modify
9 it under the terms of the GNU Lesser General Public License as published
10 by the Free Software Foundation; either version 2 of the License,
11 or (at your option) any later version.
13 PulseAudio is distributed in the hope that it will be useful, but
14 WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 General Public License for more details.
18 You should have received a copy of the GNU Lesser General Public License
19 along with PulseAudio; if not, write to the Free Software
20 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
35 #include <pulse/xmalloc.h>
37 #include <pulsecore/sink-input.h>
38 #include <pulsecore/log.h>
40 #include "sound-file-stream.h"
42 #define BUF_SIZE (1024*10)
46 pa_sink_input *sink_input;
48 sf_count_t (*readf_function)(SNDFILE *sndfile, void *ptr, sf_count_t frames);
51 static void free_userdata(struct userdata *u) {
54 pa_sink_input_disconnect(u->sink_input);
55 pa_sink_input_unref(u->sink_input);
58 if (u->memchunk.memblock)
59 pa_memblock_unref(u->memchunk.memblock);
66 static void sink_input_kill(pa_sink_input *i) {
67 assert(i && i->userdata);
68 free_userdata(i->userdata);
71 static int sink_input_peek(pa_sink_input *i, pa_memchunk *chunk) {
73 assert(i && chunk && i->userdata);
76 if (!u->memchunk.memblock) {
77 uint32_t fs = pa_frame_size(&i->sample_spec);
81 u->memchunk.memblock = pa_memblock_new(i->sink->core->mempool, BUF_SIZE);
82 u->memchunk.index = 0;
84 p = pa_memblock_acquire(u->memchunk.memblock);
86 if (u->readf_function) {
87 if ((n = u->readf_function(u->sndfile, p, BUF_SIZE/fs)) <= 0)
90 u->memchunk.length = n * fs;
92 if ((n = sf_read_raw(u->sndfile, p, BUF_SIZE)) <= 0)
95 u->memchunk.length = n;
97 pa_memblock_release(u->memchunk.memblock);
99 if (!u->memchunk.length) {
105 *chunk = u->memchunk;
106 pa_memblock_ref(chunk->memblock);
107 assert(chunk->length);
111 static void sink_input_drop(pa_sink_input *i, const pa_memchunk*chunk, size_t length) {
113 assert(i && chunk && length && i->userdata);
116 assert(!memcmp(chunk, &u->memchunk, sizeof(chunk)));
117 assert(length <= u->memchunk.length);
119 u->memchunk.index += length;
120 u->memchunk.length -= length;
122 if (u->memchunk.length <= 0) {
123 pa_memblock_unref(u->memchunk.memblock);
124 u->memchunk.memblock = NULL;
125 u->memchunk.index = u->memchunk.length = 0;
132 const pa_cvolume *volume) {
134 struct userdata *u = NULL;
137 pa_sink_input_new_data data;
142 u = pa_xnew(struct userdata, 1);
143 u->sink_input = NULL;
144 u->memchunk.memblock = NULL;
145 u->memchunk.index = u->memchunk.length = 0;
148 memset(&sfinfo, 0, sizeof(sfinfo));
150 if (!(u->sndfile = sf_open(fname, SFM_READ, &sfinfo))) {
151 pa_log("Failed to open file %s", fname);
155 u->readf_function = NULL;
157 switch (sfinfo.format & 0xFF) {
158 case SF_FORMAT_PCM_16:
159 case SF_FORMAT_PCM_U8:
160 case SF_FORMAT_PCM_S8:
161 ss.format = PA_SAMPLE_S16NE;
162 u->readf_function = (sf_count_t (*)(SNDFILE *sndfile, void *ptr, sf_count_t frames)) sf_readf_short;
166 ss.format = PA_SAMPLE_ULAW;
170 ss.format = PA_SAMPLE_ALAW;
173 case SF_FORMAT_FLOAT:
175 ss.format = PA_SAMPLE_FLOAT32NE;
176 u->readf_function = (sf_count_t (*)(SNDFILE *sndfile, void *ptr, sf_count_t frames)) sf_readf_float;
180 ss.rate = sfinfo.samplerate;
181 ss.channels = sfinfo.channels;
183 if (!pa_sample_spec_valid(&ss)) {
184 pa_log("Unsupported sample format in file %s", fname);
188 pa_sink_input_new_data_init(&data);
190 data.driver = __FILE__;
192 pa_sink_input_new_data_set_sample_spec(&data, &ss);
193 pa_sink_input_new_data_set_volume(&data, volume);
195 if (!(u->sink_input = pa_sink_input_new(sink->core, &data, 0)))
198 u->sink_input->peek = sink_input_peek;
199 u->sink_input->drop = sink_input_drop;
200 u->sink_input->kill = sink_input_kill;
201 u->sink_input->userdata = u;
203 pa_sink_notify(u->sink_input->sink);