1 /***
2 This file is part of PulseAudio.
3
4 Copyright 2004-2006 Lennart Poettering
5
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.1 of the License,
9 or (at your option) any later version.
10
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.
15
16 You should have received a copy of the GNU Lesser General Public License
17 along with PulseAudio; if not, see <http://www.gnu.org/licenses/>.
18 ***/
19
20 #ifdef HAVE_CONFIG_H
21 #include <config.h>
22 #endif
23
24 #include <unistd.h>
25 #include <fcntl.h>
26 #include <errno.h>
27
28 #include <sndfile.h>
29
30 #include <pulse/sample.h>
31 #include <pulsecore/log.h>
32 #include <pulsecore/macro.h>
33 #include <pulsecore/core-error.h>
34 #include <pulsecore/core-util.h>
35 #include <pulsecore/core-scache.h>
36 #include <pulsecore/sndfile-util.h>
37
38 #include "sound-file.h"
39
pa_sound_file_load(pa_mempool * pool,const char * fname,pa_sample_spec * ss,pa_channel_map * map,pa_memchunk * chunk,pa_proplist * p)40 int pa_sound_file_load(
41 pa_mempool *pool,
42 const char *fname,
43 pa_sample_spec *ss,
44 pa_channel_map *map,
45 pa_memchunk *chunk,
46 pa_proplist *p) {
47
48 SNDFILE *sf = NULL;
49 SF_INFO sfi;
50 int ret = -1;
51 size_t l;
52 sf_count_t (*readf_function)(SNDFILE *sndfile, void *ptr, sf_count_t frames) = NULL;
53 void *ptr = NULL;
54 int fd;
55
56 pa_assert(fname);
57 pa_assert(ss);
58 pa_assert(chunk);
59
60 pa_memchunk_reset(chunk);
61
62 if ((fd = pa_open_cloexec(fname, O_RDONLY, 0)) < 0) {
63 pa_log("Failed to open file %s: %s", fname, pa_cstrerror(errno));
64 goto finish;
65 }
66
67 #ifdef HAVE_POSIX_FADVISE
68 if (posix_fadvise(fd, 0, 0, POSIX_FADV_SEQUENTIAL) < 0) {
69 pa_log_warn("POSIX_FADV_SEQUENTIAL failed: %s", pa_cstrerror(errno));
70 goto finish;
71 } else
72 pa_log_debug("POSIX_FADV_SEQUENTIAL succeeded.");
73 #endif
74
75 pa_zero(sfi);
76 if (!(sf = sf_open_fd(fd, SFM_READ, &sfi, 1))) {
77 pa_log("Failed to open file %s", fname);
78 goto finish;
79 }
80
81 fd = -1;
82
83 if (pa_sndfile_read_sample_spec(sf, ss) < 0) {
84 pa_log("Failed to determine file sample format.");
85 goto finish;
86 }
87
88 if ((map && pa_sndfile_read_channel_map(sf, map) < 0)) {
89 if (ss->channels > 2)
90 pa_log("Failed to determine file channel map, synthesizing one.");
91 pa_channel_map_init_extend(map, ss->channels, PA_CHANNEL_MAP_DEFAULT);
92 }
93
94 if (p)
95 pa_sndfile_init_proplist(sf, p);
96
97 if ((l = pa_frame_size(ss) * (size_t) sfi.frames) > PA_SCACHE_ENTRY_SIZE_MAX) {
98 pa_log("File too large");
99 goto finish;
100 }
101
102 chunk->memblock = pa_memblock_new(pool, l);
103 chunk->index = 0;
104 chunk->length = l;
105
106 readf_function = pa_sndfile_readf_function(ss);
107
108 ptr = pa_memblock_acquire(chunk->memblock);
109
110 if ((readf_function && readf_function(sf, ptr, sfi.frames) != sfi.frames) ||
111 (!readf_function && sf_read_raw(sf, ptr, (sf_count_t) l) != (sf_count_t) l)) {
112 pa_log("Premature file end");
113 goto finish;
114 }
115
116 ret = 0;
117
118 finish:
119
120 if (sf)
121 sf_close(sf);
122
123 if (ptr)
124 pa_memblock_release(chunk->memblock);
125
126 if (ret != 0 && chunk->memblock)
127 pa_memblock_unref(chunk->memblock);
128
129 if (fd >= 0)
130 pa_close(fd);
131
132 return ret;
133 }
134
pa_sound_file_too_big_to_cache(const char * fname)135 int pa_sound_file_too_big_to_cache(const char *fname) {
136
137 SNDFILE*sf = NULL;
138 SF_INFO sfi;
139 pa_sample_spec ss;
140
141 pa_assert(fname);
142
143 pa_zero(sfi);
144 if (!(sf = sf_open(fname, SFM_READ, &sfi))) {
145 pa_log("Failed to open file %s", fname);
146 return -1;
147 }
148
149 if (pa_sndfile_read_sample_spec(sf, &ss) < 0) {
150 pa_log("Failed to determine file sample format.");
151 sf_close(sf);
152 return -1;
153 }
154
155 sf_close(sf);
156
157 if ((pa_frame_size(&ss) * (size_t) sfi.frames) > PA_SCACHE_ENTRY_SIZE_MAX) {
158 pa_log("File too large: %s", fname);
159 return 1;
160 }
161
162 return 0;
163 }
164