4 This file is part of polypaudio.
6 polypaudio 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 polypaudio 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 polypaudio; if not, write to the Free Software
18 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
33 #include "sound-file-stream.h"
34 #include "sink-input.h"
38 #define BUF_SIZE (1024*10)
42 pa_sink_input
*sink_input
;
44 sf_count_t (*readf_function
)(SNDFILE
*sndfile
, void *ptr
, sf_count_t frames
);
47 static void free_userdata(struct userdata
*u
) {
50 pa_sink_input_disconnect(u
->sink_input
);
51 pa_sink_input_unref(u
->sink_input
);
54 if (u
->memchunk
.memblock
)
55 pa_memblock_unref(u
->memchunk
.memblock
);
62 static void sink_input_kill(pa_sink_input
*i
) {
63 assert(i
&& i
->userdata
);
64 free_userdata(i
->userdata
);
67 static void si_kill(PA_GCC_UNUSED pa_mainloop_api
*m
, void *i
) {
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
);
78 sf_count_t samples
= BUF_SIZE
/fs
;
80 u
->memchunk
.memblock
= pa_memblock_new(BUF_SIZE
, i
->sink
->core
->memblock_stat
);
81 u
->memchunk
.index
= 0;
82 samples
= u
->readf_function(u
->sndfile
, u
->memchunk
.memblock
->data
, samples
);
83 u
->memchunk
.length
= samples
*fs
;
85 if (!u
->memchunk
.length
) {
86 pa_memblock_unref(u
->memchunk
.memblock
);
87 u
->memchunk
.memblock
= NULL
;
88 u
->memchunk
.index
= u
->memchunk
.length
= 0;
89 pa_mainloop_api_once(i
->sink
->core
->mainloop
, si_kill
, i
);
95 pa_memblock_ref(chunk
->memblock
);
96 assert(chunk
->length
);
100 static void sink_input_drop(pa_sink_input
*i
, const pa_memchunk
*chunk
, size_t length
) {
102 assert(i
&& chunk
&& length
&& i
->userdata
);
105 assert(!memcmp(chunk
, &u
->memchunk
, sizeof(chunk
)));
106 assert(length
<= u
->memchunk
.length
);
108 u
->memchunk
.index
+= length
;
109 u
->memchunk
.length
-= length
;
111 if (u
->memchunk
.length
<= 0) {
112 pa_memblock_unref(u
->memchunk
.memblock
);
113 u
->memchunk
.memblock
= NULL
;
114 u
->memchunk
.index
= u
->memchunk
.length
= 0;
118 int pa_play_file(pa_sink
*sink
, const char *fname
, const pa_cvolume
*volume
) {
119 struct userdata
*u
= NULL
;
122 assert(sink
&& fname
);
124 u
= pa_xmalloc(sizeof(struct userdata
));
125 u
->sink_input
= NULL
;
126 u
->memchunk
.memblock
= NULL
;
127 u
->memchunk
.index
= u
->memchunk
.length
= 0;
130 memset(&sfinfo
, 0, sizeof(sfinfo
));
132 if (!(u
->sndfile
= sf_open(fname
, SFM_READ
, &sfinfo
))) {
133 pa_log(__FILE__
": Failed to open file %s\n", fname
);
137 switch (sfinfo
.format
& 0xFF) {
138 case SF_FORMAT_PCM_16
:
139 case SF_FORMAT_PCM_U8
:
142 ss
.format
= PA_SAMPLE_S16NE
;
143 u
->readf_function
= (sf_count_t (*)(SNDFILE
*sndfile
, void *ptr
, sf_count_t frames
)) sf_readf_short
;
145 case SF_FORMAT_FLOAT
:
147 ss
.format
= PA_SAMPLE_FLOAT32NE
;
148 u
->readf_function
= (sf_count_t (*)(SNDFILE
*sndfile
, void *ptr
, sf_count_t frames
)) sf_readf_float
;
152 ss
.rate
= sfinfo
.samplerate
;
153 ss
.channels
= sfinfo
.channels
;
155 if (!pa_sample_spec_valid(&ss
)) {
156 pa_log(__FILE__
": Unsupported sample format in file %s\n", fname
);
160 if (!(u
->sink_input
= pa_sink_input_new(sink
, __FILE__
, fname
, &ss
, NULL
, 0, -1)))
164 u
->sink_input
->volume
= *volume
;
165 u
->sink_input
->peek
= sink_input_peek
;
166 u
->sink_input
->drop
= sink_input_drop
;
167 u
->sink_input
->kill
= sink_input_kill
;
168 u
->sink_input
->userdata
= u
;
170 pa_sink_notify(sink
);