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(BUF_SIZE
, i
->sink
->core
->memblock_stat
);
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;
123 int pa_play_file(pa_sink
*sink
, const char *fname
, const pa_cvolume
*volume
) {
124 struct userdata
*u
= NULL
;
127 assert(sink
&& fname
);
129 u
= pa_xmalloc(sizeof(struct userdata
));
130 u
->sink_input
= NULL
;
131 u
->memchunk
.memblock
= NULL
;
132 u
->memchunk
.index
= u
->memchunk
.length
= 0;
135 memset(&sfinfo
, 0, sizeof(sfinfo
));
137 if (!(u
->sndfile
= sf_open(fname
, SFM_READ
, &sfinfo
))) {
138 pa_log(__FILE__
": Failed to open file %s", fname
);
142 u
->readf_function
= NULL
;
144 switch (sfinfo
.format
& 0xFF) {
145 case SF_FORMAT_PCM_16
:
146 case SF_FORMAT_PCM_U8
:
147 case SF_FORMAT_PCM_S8
:
148 ss
.format
= PA_SAMPLE_S16NE
;
149 u
->readf_function
= (sf_count_t (*)(SNDFILE
*sndfile
, void *ptr
, sf_count_t frames
)) sf_readf_short
;
153 ss
.format
= PA_SAMPLE_ULAW
;
157 ss
.format
= PA_SAMPLE_ALAW
;
160 case SF_FORMAT_FLOAT
:
162 ss
.format
= PA_SAMPLE_FLOAT32NE
;
163 u
->readf_function
= (sf_count_t (*)(SNDFILE
*sndfile
, void *ptr
, sf_count_t frames
)) sf_readf_float
;
167 ss
.rate
= sfinfo
.samplerate
;
168 ss
.channels
= sfinfo
.channels
;
170 if (!pa_sample_spec_valid(&ss
)) {
171 pa_log(__FILE__
": Unsupported sample format in file %s", fname
);
175 if (!(u
->sink_input
= pa_sink_input_new(sink
, __FILE__
, fname
, &ss
, NULL
, volume
, 0, -1)))
178 u
->sink_input
->peek
= sink_input_peek
;
179 u
->sink_input
->drop
= sink_input_drop
;
180 u
->sink_input
->kill
= sink_input_kill
;
181 u
->sink_input
->userdata
= u
;
183 pa_sink_notify(sink
);