]>
code.delx.au - pulseaudio/blob - polyp/sound-file-stream.c
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 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 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 struct pa_sink_input
*sink_input
;
43 struct pa_memchunk memchunk
;
46 static void free_userdata(struct userdata
*u
) {
49 pa_sink_input_disconnect(u
->sink_input
);
50 pa_sink_input_unref(u
->sink_input
);
53 if (u
->memchunk
.memblock
)
54 pa_memblock_unref(u
->memchunk
.memblock
);
61 static void sink_input_kill(struct pa_sink_input
*i
) {
62 assert(i
&& i
->userdata
);
63 free_userdata(i
->userdata
);
66 static void si_kill(struct pa_mainloop_api
*m
, void *i
) {
70 static int sink_input_peek(struct pa_sink_input
*i
, struct pa_memchunk
*chunk
) {
72 assert(i
&& chunk
&& i
->userdata
);
75 if (!u
->memchunk
.memblock
) {
76 uint32_t fs
= pa_frame_size(&i
->sample_spec
);
77 sf_count_t samples
= BUF_SIZE
/fs
;
79 u
->memchunk
.memblock
= pa_memblock_new(BUF_SIZE
, i
->sink
->core
->memblock_stat
);
80 u
->memchunk
.index
= 0;
81 samples
= sf_readf_float(u
->sndfile
, u
->memchunk
.memblock
->data
, samples
);
82 u
->memchunk
.length
= samples
*fs
;
84 if (!u
->memchunk
.length
) {
85 pa_memblock_unref(u
->memchunk
.memblock
);
86 u
->memchunk
.memblock
= NULL
;
87 u
->memchunk
.index
= u
->memchunk
.length
= 0;
88 pa_mainloop_api_once(i
->sink
->core
->mainloop
, si_kill
, i
);
94 pa_memblock_ref(chunk
->memblock
);
95 assert(chunk
->length
);
99 static void sink_input_drop(struct pa_sink_input
*i
, const struct pa_memchunk
*chunk
, size_t length
) {
101 assert(i
&& chunk
&& length
&& i
->userdata
);
104 assert(!memcmp(chunk
, &u
->memchunk
, sizeof(chunk
)));
105 assert(length
<= u
->memchunk
.length
);
107 u
->memchunk
.index
+= length
;
108 u
->memchunk
.length
-= length
;
110 if (u
->memchunk
.length
<= 0) {
111 pa_memblock_unref(u
->memchunk
.memblock
);
112 u
->memchunk
.memblock
= NULL
;
113 u
->memchunk
.index
= u
->memchunk
.length
= 0;
117 int pa_play_file(struct pa_sink
*sink
, const char *fname
, pa_volume_t volume
) {
118 struct userdata
*u
= NULL
;
120 struct pa_sample_spec ss
;
121 assert(sink
&& fname
);
126 u
= pa_xmalloc(sizeof(struct userdata
));
127 u
->sink_input
= NULL
;
128 u
->memchunk
.memblock
= NULL
;
129 u
->memchunk
.index
= u
->memchunk
.length
= 0;
132 memset(&sfinfo
, 0, sizeof(sfinfo
));
134 if (!(u
->sndfile
= sf_open(fname
, SFM_READ
, &sfinfo
))) {
135 pa_log(__FILE__
": Failed to open file %s\n", fname
);
139 ss
.format
= PA_SAMPLE_FLOAT32
;
140 ss
.rate
= sfinfo
.samplerate
;
141 ss
.channels
= sfinfo
.channels
;
143 if (!pa_sample_spec_valid(&ss
)) {
144 pa_log(__FILE__
": Unsupported sample format in file %s\n", fname
);
148 if (!(u
->sink_input
= pa_sink_input_new(sink
, fname
, &ss
, 0)))
151 u
->sink_input
->volume
= volume
;
152 u
->sink_input
->peek
= sink_input_peek
;
153 u
->sink_input
->drop
= sink_input_drop
;
154 u
->sink_input
->kill
= sink_input_kill
;
155 u
->sink_input
->userdata
= u
;
157 pa_sink_notify(sink
);