consolidate close() calls to pa_close(), and make sure on every occasion that we...
[platform/upstream/pulseaudio.git] / src / pulsecore / sound-file.c
1 /* $Id$ */
2
3 /***
4   This file is part of PulseAudio.
5
6   Copyright 2004-2006 Lennart Poettering
7
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.
12
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.
17
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
21   USA.
22 ***/
23
24 #ifdef HAVE_CONFIG_H
25 #include <config.h>
26 #endif
27
28 #include <string.h>
29 #include <unistd.h>
30 #include <fcntl.h>
31 #include <errno.h>
32
33 #include <sndfile.h>
34
35 #include <pulse/sample.h>
36 #include <pulsecore/log.h>
37 #include <pulsecore/macro.h>
38 #include <pulsecore/core-error.h>
39 #include <pulsecore/core-util.h>
40
41 #include "sound-file.h"
42 #include "core-scache.h"
43
44 int pa_sound_file_load(
45         pa_mempool *pool,
46         const char *fname,
47         pa_sample_spec *ss,
48         pa_channel_map *map,
49         pa_memchunk *chunk) {
50     
51     SNDFILE *sf = NULL;
52     SF_INFO sfinfo;
53     int ret = -1;
54     size_t l;
55     sf_count_t (*readf_function)(SNDFILE *sndfile, void *ptr, sf_count_t frames) = NULL;
56     void *ptr = NULL;
57     int fd;
58
59     pa_assert(fname);
60     pa_assert(ss);
61     pa_assert(chunk);
62
63     pa_memchunk_reset(chunk);
64     memset(&sfinfo, 0, sizeof(sfinfo));
65
66     if ((fd = open(fname, O_RDONLY|O_NOCTTY)) < 0) {
67         pa_log("Failed to open file %s: %s", fname, pa_cstrerror(errno));
68         goto finish;
69     }
70
71 #ifdef HAVE_POSIX_FADVISE
72     if (posix_fadvise(fd, 0, 0, POSIX_FADV_SEQUENTIAL) < 0) {
73         pa_log_warn("POSIX_FADV_SEQUENTIAL failed: %s", pa_cstrerror(errno));
74         goto finish;
75     } else
76         pa_log_debug("POSIX_FADV_SEQUENTIAL succeeded.");
77 #endif
78     
79     if (!(sf = sf_open_fd(fd, SFM_READ, &sfinfo, 1))) {
80         pa_log("Failed to open file %s", fname);
81         pa_close(fd);
82         goto finish;
83     }
84
85     switch (sfinfo.format & SF_FORMAT_SUBMASK) {
86         case SF_FORMAT_PCM_16:
87         case SF_FORMAT_PCM_U8:
88         case SF_FORMAT_PCM_S8:
89             ss->format = PA_SAMPLE_S16NE;
90             readf_function = (sf_count_t (*)(SNDFILE *sndfile, void *ptr, sf_count_t frames)) sf_readf_short;
91             break;
92
93         case SF_FORMAT_ULAW:
94             ss->format = PA_SAMPLE_ULAW;
95             break;
96
97         case SF_FORMAT_ALAW:
98             ss->format = PA_SAMPLE_ALAW;
99             break;
100
101         case SF_FORMAT_FLOAT:
102         case SF_FORMAT_DOUBLE:
103         default:
104             ss->format = PA_SAMPLE_FLOAT32NE;
105             readf_function = (sf_count_t (*)(SNDFILE *sndfile, void *ptr, sf_count_t frames)) sf_readf_float;
106             break;
107     }
108
109     ss->rate = sfinfo.samplerate;
110     ss->channels = sfinfo.channels;
111
112     if (!pa_sample_spec_valid(ss)) {
113         pa_log("Unsupported sample format in file %s", fname);
114         goto finish;
115     }
116
117     if (map)
118         pa_channel_map_init_auto(map, ss->channels, PA_CHANNEL_MAP_DEFAULT);
119
120     if ((l = pa_frame_size(ss) * sfinfo.frames) > PA_SCACHE_ENTRY_SIZE_MAX) {
121         pa_log("File too large");
122         goto finish;
123     }
124
125     chunk->memblock = pa_memblock_new(pool, l);
126     chunk->index = 0;
127     chunk->length = l;
128
129     ptr = pa_memblock_acquire(chunk->memblock);
130
131     if ((readf_function && readf_function(sf, ptr, sfinfo.frames) != sfinfo.frames) ||
132         (!readf_function && sf_read_raw(sf, ptr, l) != (sf_count_t) l)) {
133         pa_log("Premature file end");
134         goto finish;
135     }
136
137     ret = 0;
138
139 finish:
140
141     if (sf)
142         sf_close(sf);
143
144     if (ptr)
145         pa_memblock_release(chunk->memblock);
146
147     if (ret != 0 && chunk->memblock)
148         pa_memblock_unref(chunk->memblock);
149
150     return ret;
151 }
152
153 int pa_sound_file_too_big_to_cache(const char *fname) {
154     
155     SNDFILE*sf = NULL;
156     SF_INFO sfinfo;
157     pa_sample_spec ss;
158
159     pa_assert(fname);
160     
161     if (!(sf = sf_open(fname, SFM_READ, &sfinfo))) {
162         pa_log("Failed to open file %s", fname);
163         return -1;
164     }
165
166     sf_close(sf);
167
168     switch (sfinfo.format & SF_FORMAT_SUBMASK) {
169         case SF_FORMAT_PCM_16:
170         case SF_FORMAT_PCM_U8:
171         case SF_FORMAT_PCM_S8:
172             ss.format = PA_SAMPLE_S16NE;
173             break;
174
175         case SF_FORMAT_ULAW:
176             ss.format = PA_SAMPLE_ULAW;
177             break;
178
179         case SF_FORMAT_ALAW:
180             ss.format = PA_SAMPLE_ALAW;
181             break;
182
183         case SF_FORMAT_DOUBLE:
184         case SF_FORMAT_FLOAT:
185         default:
186             ss.format = PA_SAMPLE_FLOAT32NE;
187             break;
188     }
189
190     ss.rate = sfinfo.samplerate;
191     ss.channels = sfinfo.channels;
192
193     if (!pa_sample_spec_valid(&ss)) {
194         pa_log("Unsupported sample format in file %s", fname);
195         return -1;
196     }
197     
198     if ((pa_frame_size(&ss) * sfinfo.frames) > PA_SCACHE_ENTRY_SIZE_MAX) {
199         pa_log("File too large: %s", fname);
200         return 1;
201     }
202
203     return 0;
204 }