1 /*
2  * Copyright (C) 2008-2017 Paul Davis <paul@linuxaudiosystems.com>
3  * Copyright (C) 2019 Robin Gareus <robin@gareus.org>
4  *
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.
9  *
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.
14  *
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.
18  */
19 
20 #ifndef _ardour_mp3file_importable_source_h_
21 #define _ardour_mp3file_importable_source_h_
22 
23 #include <stdint.h>
24 
25 #include "ardour/libardour_visibility.h"
26 #include "ardour/types.h"
27 #include "ardour/importable_source.h"
28 
29 namespace ARDOUR {
30 
31 /* 64bit CPUs always have SSE2, armhf/aarch64 has NEON,
32  * except apple/OSX does not provide immintrin.h
33  */
34 #if defined ( __i386__) || defined  (__PPC__) || defined (__APPLE__)
35 #define MINIMP3_NO_SIMD // disable for portability
36 #endif
37 
38 #define MINIMP3_NONSTANDARD_BUT_LOGICAL
39 #define MINIMP3_FLOAT_OUTPUT
40 
41 /* use Ardour namespace for minimp3 symbols */
42 #include "ardour/minimp3.h"
43 
44 class LIBARDOUR_API Mp3FileImportableSource : public ImportableSource {
45 public:
46 	Mp3FileImportableSource (const std::string& path);
47 	virtual ~Mp3FileImportableSource();
48 
49 	/* ImportableSource API */
channels()50 	uint32_t    channels () const { return _info.channels; }
length()51 	samplecnt_t length () const { return _length; }
samplerate()52 	samplecnt_t samplerate () const { return _info.hz; }
natural_position()53 	samplepos_t natural_position () const { return 0 ; }
54 	void        seek (samplepos_t pos);
55 	samplecnt_t read (Sample*, samplecnt_t nframes);
56 
clamped_at_unity()57 	bool clamped_at_unity () const { return false; }
58 
layer()59 	samplecnt_t layer () const { return _info.layer; }
bitrate()60 	samplecnt_t bitrate () const { return _info.bitrate_kbps; }
61 	samplecnt_t read_unlocked (Sample*, samplepos_t start, samplecnt_t cnt, uint32_t chn);
62 
63 private:
64 	void unmap_mem ();
65 	int  decode_mp3 (bool parse_only = false);
66 
67 	mp3dec_t            _mp3d;
68 	mp3dec_frame_info_t _info;
69 	samplecnt_t         _length;
70 
71 	int            _fd;
72 	const uint8_t* _map_addr;
73 	size_t         _map_length;
74 
75 	const uint8_t* _buffer;
76 	size_t         _remain;
77 
78 	samplepos_t   _read_position;
79 	mp3d_sample_t _pcm[MINIMP3_MAX_SAMPLES_PER_FRAME];
80 	size_t        _pcm_off;
81 	int           _n_frames;
82 };
83 
84 }
85 
86 #endif /* __ardour_mp3file_importable_source_h__ */
87