2 * Copyright (C) 2008-2017 Paul Davis <paul@linuxaudiosystems.com>
3 * Copyright (C) 2019 Robin Gareus <robin@gareus.org>
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
15 * You should have received a copy of the GNU General Public License along
16 * with this program; if not, write to the Free Software Foundation, Inc.,
17 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
20 #define MINIMP3_IMPLEMENTATION
24 #ifdef PLATFORM_WINDOWS
31 #include "pbd/gstdio_compat.h"
33 #include "pbd/error.h"
34 #include "ardour/mp3fileimportable.h"
36 using namespace ARDOUR;
39 Mp3FileImportableSource::Mp3FileImportableSource (const string& path)
50 memset (&_info, 0, sizeof (_info));
53 if (g_stat (path.c_str(), &statbuf) != 0) {
54 throw failed_constructor ();
57 _fd = g_open (path.c_str (), O_RDONLY, 0444);
59 throw failed_constructor ();
61 _map_length = statbuf.st_size;
63 #ifdef PLATFORM_WINDOWS
64 HANDLE file_handle = (HANDLE) _get_osfhandle (int(_fd));
66 HANDLE map_handle = CreateFileMapping (file_handle, NULL, PAGE_READONLY, 0, 0, NULL);
67 if (map_handle == NULL) {
69 throw failed_constructor ();
72 LPVOID view_handle = MapViewOfFile (map_handle, FILE_MAP_READ, 0, 0, _map_length);
73 if (view_handle == NULL) {
74 CloseHandle (map_handle);
76 throw failed_constructor ();
78 _map_addr = (const uint8_t*)view_handle;
79 CloseHandle (map_handle);
81 _map_addr = (const uint8_t *)mmap (NULL, _map_length, PROT_READ, MAP_PRIVATE, _fd, 0);
82 if (_map_addr == MAP_FAILED) {
84 throw failed_constructor ();
89 _remain = _map_length;
93 throw failed_constructor ();
96 /* estimate length, fixed bitrate */
97 _length = _n_frames * _map_length / _info.frame_bytes;
99 #if 1 /* detect accurate length by parsing frame headers */
101 while (decode_mp3 (true)) {
102 _length += _n_frames;
104 _read_position = _length;
109 Mp3FileImportableSource::~Mp3FileImportableSource ()
115 Mp3FileImportableSource::unmap_mem ()
117 #ifdef PLATFORM_WINDOWS
119 UnmapViewOfFile (_map_addr);
122 munmap ((void*) _map_addr, _map_length);
129 Mp3FileImportableSource::decode_mp3 (bool parse_only)
133 _n_frames = mp3dec_decode_frame (&_mp3d, _buffer, _remain, parse_only ? NULL :_pcm, &_info);
134 _buffer += _info.frame_bytes;
135 _remain -= _info.frame_bytes;
139 } while (_info.frame_bytes);
144 Mp3FileImportableSource::seek (samplepos_t pos)
146 if (_read_position == pos) {
150 /* rewind, then decode to pos */
151 if (pos < _read_position) {
153 _remain = _map_length;
156 mp3dec_init (&_mp3d);
160 while (_read_position + _n_frames <= pos) {
161 if (!decode_mp3 ()) {
164 _read_position += _n_frames;
168 _pcm_off = _info.channels * (pos - _read_position);
169 _n_frames -= pos - _read_position;
170 _read_position = pos;
172 assert (_pcm_off >= 0 && _pcm_off < MINIMP3_MAX_SAMPLES_PER_FRAME);
176 Mp3FileImportableSource::read (Sample* dst, samplecnt_t nframes)
179 int remain = nframes; // == samples * channels
181 samplecnt_t samples_to_copy = std::min (remain, _n_frames * _info.channels);
182 if (samples_to_copy > 0) {
183 memcpy (&dst[dst_off], &_pcm[_pcm_off], samples_to_copy * sizeof (Sample));
184 remain -= samples_to_copy;
185 dst_off += samples_to_copy;
186 _n_frames -= samples_to_copy / _info.channels;
187 _pcm_off += samples_to_copy;
188 _read_position += samples_to_copy / _info.channels;
190 assert (_n_frames >= 0);
191 if (_n_frames <= 0 && !decode_mp3 ()) {
192 /* EOF, or decode error */
200 Mp3FileImportableSource::read_unlocked (Sample* dst, samplepos_t start, samplecnt_t cnt, uint32_t chn)
202 const uint32_t n_chn = channels ();
203 if (chn > n_chn || cnt == 0) {
206 if (start != _read_position) {
211 samplecnt_t remain = cnt;
214 samplecnt_t samples_to_copy = std::min (remain, (samplecnt_t)_n_frames);
216 for (samplecnt_t n = 0; n < samples_to_copy; ++n) {
217 dst[dst_off] = _pcm[_pcm_off + chn];
225 assert (_n_frames >= 0);
226 if (_n_frames <= 0 && !decode_mp3 ()) {
227 /* EOF, or decode error */