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
);
80 u
->memchunk
.memblock
= pa_memblock_new(i
->sink
->core
->mempool
, BUF_SIZE
);
81 u
->memchunk
.index
= 0;
83 if (u
->readf_function
) {
84 if ((n
= u
->readf_function(u
->sndfile
, u
->memchunk
.memblock
->data
, BUF_SIZE
/fs
)) <= 0)
87 u
->memchunk
.length
= n
* fs
;
89 if ((n
= sf_read_raw(u
->sndfile
, u
->memchunk
.memblock
->data
, BUF_SIZE
)) <= 0)
92 u
->memchunk
.length
= n
;
95 if (!u
->memchunk
.length
) {
101 *chunk
= u
->memchunk
;
102 pa_memblock_ref(chunk
->memblock
);
103 assert(chunk
->length
);
107 static void sink_input_drop(pa_sink_input
*i
, const pa_memchunk
*chunk
, size_t length
) {
109 assert(i
&& chunk
&& length
&& i
->userdata
);
112 assert(!memcmp(chunk
, &u
->memchunk
, sizeof(chunk
)));
113 assert(length
<= u
->memchunk
.length
);
115 u
->memchunk
.index
+= length
;
116 u
->memchunk
.length
-= length
;
118 if (u
->memchunk
.length
<= 0) {
119 pa_memblock_unref(u
->memchunk
.memblock
);
120 u
->memchunk
.memblock
= NULL
;
121 u
->memchunk
.index
= u
->memchunk
.length
= 0;
128 const pa_cvolume
*volume
) {
130 struct userdata
*u
= NULL
;
133 pa_sink_input_new_data data
;
138 u
= pa_xnew(struct userdata
, 1);
139 u
->sink_input
= NULL
;
140 u
->memchunk
.memblock
= NULL
;
141 u
->memchunk
.index
= u
->memchunk
.length
= 0;
144 memset(&sfinfo
, 0, sizeof(sfinfo
));
146 if (!(u
->sndfile
= sf_open(fname
, SFM_READ
, &sfinfo
))) {
147 pa_log("Failed to open file %s", fname
);
151 u
->readf_function
= NULL
;
153 switch (sfinfo
.format
& 0xFF) {
154 case SF_FORMAT_PCM_16
:
155 case SF_FORMAT_PCM_U8
:
156 case SF_FORMAT_PCM_S8
:
157 ss
.format
= PA_SAMPLE_S16NE
;
158 u
->readf_function
= (sf_count_t (*)(SNDFILE
*sndfile
, void *ptr
, sf_count_t frames
)) sf_readf_short
;
162 ss
.format
= PA_SAMPLE_ULAW
;
166 ss
.format
= PA_SAMPLE_ALAW
;
169 case SF_FORMAT_FLOAT
:
171 ss
.format
= PA_SAMPLE_FLOAT32NE
;
172 u
->readf_function
= (sf_count_t (*)(SNDFILE
*sndfile
, void *ptr
, sf_count_t frames
)) sf_readf_float
;
176 ss
.rate
= sfinfo
.samplerate
;
177 ss
.channels
= sfinfo
.channels
;
179 if (!pa_sample_spec_valid(&ss
)) {
180 pa_log("Unsupported sample format in file %s", fname
);
184 pa_sink_input_new_data_init(&data
);
186 data
.driver
= __FILE__
;
188 pa_sink_input_new_data_set_sample_spec(&data
, &ss
);
189 pa_sink_input_new_data_set_volume(&data
, volume
);
191 if (!(u
->sink_input
= pa_sink_input_new(sink
->core
, &data
, 0)))
194 u
->sink_input
->peek
= sink_input_peek
;
195 u
->sink_input
->drop
= sink_input_drop
;
196 u
->sink_input
->kill
= sink_input_kill
;
197 u
->sink_input
->userdata
= u
;
199 pa_sink_notify(u
->sink_input
->sink
);