Implement mp3 import, using minimp3
This commit is contained in:
parent
bef74c267e
commit
d0b6c437ce
File diff suppressed because it is too large
Load Diff
|
@ -0,0 +1,82 @@
|
|||
/*
|
||||
* Copyright (C) 2008-2017 Paul Davis <paul@linuxaudiosystems.com>
|
||||
* Copyright (C) 2019 Robin Gareus <robin@gareus.org>
|
||||
*
|
||||
* This program is free software; you can redistribute it and/or modify
|
||||
* it under the terms of the GNU General Public License as published by
|
||||
* the Free Software Foundation; either version 2 of the License, or
|
||||
* (at your option) any later version.
|
||||
*
|
||||
* This program is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
* GNU General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU General Public License along
|
||||
* with this program; if not, write to the Free Software Foundation, Inc.,
|
||||
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
|
||||
*/
|
||||
|
||||
#ifndef _ardour_mp3file_importable_source_h_
|
||||
#define _ardour_mp3file_importable_source_h_
|
||||
|
||||
#include <boost/shared_ptr.hpp>
|
||||
#include "ardour/libardour_visibility.h"
|
||||
#include "ardour/types.h"
|
||||
#include "ardour/importable_source.h"
|
||||
|
||||
namespace ARDOUR {
|
||||
|
||||
#if defined ( __i386__) || defined (__PPC__)
|
||||
#define MINIMP3_NO_SIMD // for portability
|
||||
#endif
|
||||
|
||||
#define MINIMP3_NONSTANDARD_BUT_LOGICAL
|
||||
#define MINIMP3_FLOAT_OUTPUT
|
||||
|
||||
/* use Ardour namespace for minimp3 symbols */
|
||||
#include "ardour/minimp3.h"
|
||||
|
||||
class LIBARDOUR_API Mp3FileImportableSource : public ImportableSource {
|
||||
public:
|
||||
Mp3FileImportableSource (const std::string& path);
|
||||
virtual ~Mp3FileImportableSource();
|
||||
|
||||
/* ImportableSource API */
|
||||
uint32_t channels () const { return _info.channels; }
|
||||
samplecnt_t length () const { return _length; }
|
||||
samplecnt_t samplerate () const { return _info.hz; }
|
||||
samplepos_t natural_position () const { return 0 ; }
|
||||
void seek (samplepos_t pos);
|
||||
samplecnt_t read (Sample*, samplecnt_t nframes);
|
||||
|
||||
bool clamped_at_unity () const { return false; }
|
||||
|
||||
samplecnt_t layer () const { return _info.layer; }
|
||||
samplecnt_t bitrate () const { return _info.bitrate_kbps; }
|
||||
samplecnt_t read_unlocked (Sample*, samplepos_t start, samplecnt_t cnt, uint32_t chn);
|
||||
|
||||
private:
|
||||
void unmap_mem ();
|
||||
int decode_mp3 ();
|
||||
|
||||
mp3dec_t _mp3d;
|
||||
mp3dec_frame_info_t _info;
|
||||
samplecnt_t _length;
|
||||
|
||||
int _fd;
|
||||
const uint8_t* _map_addr;
|
||||
size_t _map_length;
|
||||
|
||||
const uint8_t* _buffer;
|
||||
size_t _remain;
|
||||
|
||||
samplepos_t _read_position;
|
||||
mp3d_sample_t _pcm[MINIMP3_MAX_SAMPLES_PER_FRAME];
|
||||
size_t _pcm_off;
|
||||
int _n_frames;
|
||||
};
|
||||
|
||||
}
|
||||
|
||||
#endif /* __ardour_mp3file_importable_source_h__ */
|
|
@ -0,0 +1,60 @@
|
|||
/*
|
||||
* Copyright (C) 2019 Robin Gareus <robin@gareus.org>
|
||||
*
|
||||
* This program is free software; you can redistribute it and/or modify
|
||||
* it under the terms of the GNU General Public License as published by
|
||||
* the Free Software Foundation; either version 2 of the License, or
|
||||
* (at your option) any later version.
|
||||
*
|
||||
* This program is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
* GNU General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU General Public License along
|
||||
* with this program; if not, write to the Free Software Foundation, Inc.,
|
||||
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
|
||||
*/
|
||||
|
||||
#ifndef _ardour_mp3file_source_h_
|
||||
#define _ardour_mp3file_source_h_
|
||||
|
||||
#include "ardour/audiofilesource.h"
|
||||
#include "ardour/mp3fileimportable.h"
|
||||
#include <string>
|
||||
|
||||
using namespace std;
|
||||
|
||||
namespace ARDOUR {
|
||||
|
||||
class LIBARDOUR_API Mp3FileSource : public AudioFileSource {
|
||||
public:
|
||||
Mp3FileSource (ARDOUR::Session&, const string& path, int chn, Flag);
|
||||
~Mp3FileSource ();
|
||||
|
||||
/* AudioSource API */
|
||||
float sample_rate() const { return _mp3.samplerate (); }
|
||||
bool clamped_at_unity () const { return false; }
|
||||
|
||||
/* AudioFileSource API */
|
||||
void flush () {}
|
||||
int update_header (samplepos_t when, struct tm&, time_t) { return 0; }
|
||||
int flush_header () { return 0; }
|
||||
void set_header_natural_position () {};
|
||||
|
||||
static int get_soundfile_info (string path, SoundFileInfo& _info, string& error_msg);
|
||||
|
||||
protected:
|
||||
/* FileSource API */
|
||||
void close ();
|
||||
/* AudioSource API */
|
||||
samplecnt_t read_unlocked (Sample *dst, samplepos_t start, samplecnt_t cnt) const;
|
||||
samplecnt_t write_unlocked (Sample *, samplecnt_t) { return 0; }
|
||||
|
||||
private:
|
||||
mutable Mp3FileImportableSource _mp3;
|
||||
int _channel;
|
||||
};
|
||||
|
||||
};
|
||||
#endif
|
|
@ -52,6 +52,7 @@
|
|||
|
||||
#include "ardour/audiofilesource.h"
|
||||
#include "ardour/debug.h"
|
||||
#include "ardour/mp3filesource.h"
|
||||
#include "ardour/sndfilesource.h"
|
||||
#include "ardour/session.h"
|
||||
#include "ardour/filename_extensions.h"
|
||||
|
@ -204,6 +205,10 @@ AudioFileSource::get_soundfile_info (const string& path, SoundFileInfo& _info, s
|
|||
}
|
||||
#endif // HAVE_COREAUDIO
|
||||
|
||||
if (Mp3FileSource::get_soundfile_info (path, _info, error_msg) == 0) {
|
||||
return true;
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
|
|
|
@ -54,6 +54,7 @@
|
|||
#include "ardour/audioengine.h"
|
||||
#include "ardour/audioregion.h"
|
||||
#include "ardour/import_status.h"
|
||||
#include "ardour/mp3fileimportable.h"
|
||||
#include "ardour/region_factory.h"
|
||||
#include "ardour/resampled_source.h"
|
||||
#include "ardour/runtime_functions.h"
|
||||
|
@ -110,6 +111,18 @@ open_importable_source (const string& path, samplecnt_t samplerate, ARDOUR::SrcQ
|
|||
} catch (...) { }
|
||||
#endif
|
||||
|
||||
/* libsndfile and CoreAudioFile failed, try minimp3-decoder */
|
||||
try {
|
||||
boost::shared_ptr<Mp3FileImportableSource> source(new Mp3FileImportableSource(path));
|
||||
|
||||
if (source->samplerate() == samplerate) {
|
||||
return source;
|
||||
}
|
||||
|
||||
/* rewrap as a resampled source */
|
||||
return boost::shared_ptr<ImportableSource>(new ResampledImportableSource(source, samplerate, quality));
|
||||
} catch (...) { }
|
||||
|
||||
throw failed_constructor ();
|
||||
}
|
||||
|
||||
|
|
|
@ -0,0 +1,223 @@
|
|||
/*
|
||||
* Copyright (C) 2008-2017 Paul Davis <paul@linuxaudiosystems.com>
|
||||
* Copyright (C) 2019 Robin Gareus <robin@gareus.org>
|
||||
*
|
||||
* This program is free software; you can redistribute it and/or modify
|
||||
* it under the terms of the GNU General Public License as published by
|
||||
* the Free Software Foundation; either version 2 of the License, or
|
||||
* (at your option) any later version.
|
||||
*
|
||||
* This program is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
* GNU General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU General Public License along
|
||||
* with this program; if not, write to the Free Software Foundation, Inc.,
|
||||
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
|
||||
*/
|
||||
|
||||
#define MINIMP3_IMPLEMENTATION
|
||||
|
||||
#include <fcntl.h>
|
||||
|
||||
#ifdef PLATFORM_WINDOWS
|
||||
#include <windows.h>
|
||||
#else
|
||||
#include <sys/mman.h>
|
||||
#endif
|
||||
|
||||
#include <glib.h>
|
||||
#include "pbd/gstdio_compat.h"
|
||||
|
||||
#include "pbd/error.h"
|
||||
#include "ardour/mp3fileimportable.h"
|
||||
|
||||
using namespace ARDOUR;
|
||||
using namespace std;
|
||||
|
||||
Mp3FileImportableSource::Mp3FileImportableSource (const string& path)
|
||||
: _fd (-1)
|
||||
, _map_addr (0)
|
||||
, _map_length (0)
|
||||
, _buffer (0)
|
||||
, _remain (0)
|
||||
, _read_position (0)
|
||||
, _pcm_off (0)
|
||||
, _n_frames (0)
|
||||
{
|
||||
mp3dec_init (&_mp3d);
|
||||
memset (&_info, 0, sizeof (_info));
|
||||
|
||||
GStatBuf statbuf;
|
||||
if (g_stat (path.c_str(), &statbuf) != 0) {
|
||||
throw failed_constructor ();
|
||||
}
|
||||
|
||||
_fd = g_open (path.c_str (), O_RDONLY, 0444);
|
||||
if (_fd == -1) {
|
||||
throw failed_constructor ();
|
||||
}
|
||||
_map_length = statbuf.st_size;
|
||||
|
||||
#ifdef PLATFORM_WINDOWS
|
||||
HANDLE file_handle = (HANDLE) _get_osfhandle (int(_fd));
|
||||
|
||||
HANDLE map_handle = CreateFileMapping (file_handle, NULL, PAGE_READONLY, 0, 0, NULL);
|
||||
if (map_handle == NULL) {
|
||||
close (_fd);
|
||||
throw failed_constructor ();
|
||||
}
|
||||
|
||||
LPVOID view_handle = MapViewOfFile (map_handle, FILE_MAP_READ, 0, 0, _map_length);
|
||||
if (view_handle == NULL) {
|
||||
CloseHandle (map_handle);
|
||||
close (_fd);
|
||||
throw failed_constructor ();
|
||||
return -1;
|
||||
}
|
||||
_map_addr = (const uint8_t*)view_handle;
|
||||
CloseHandle (map_handle);
|
||||
#else
|
||||
_map_addr = (const uint8_t *)mmap (NULL, _map_length, PROT_READ, MAP_PRIVATE, _fd, 0);
|
||||
if (_map_addr == MAP_FAILED) {
|
||||
close (_fd);
|
||||
throw failed_constructor ();
|
||||
}
|
||||
#endif
|
||||
|
||||
_buffer = _map_addr;
|
||||
_remain = _map_length;
|
||||
|
||||
if (!decode_mp3 ()) {
|
||||
unmap_mem ();
|
||||
throw failed_constructor ();
|
||||
}
|
||||
|
||||
_length = _n_frames * _map_length / _info.frame_bytes;
|
||||
}
|
||||
|
||||
Mp3FileImportableSource::~Mp3FileImportableSource ()
|
||||
{
|
||||
unmap_mem ();
|
||||
}
|
||||
|
||||
void
|
||||
Mp3FileImportableSource::unmap_mem ()
|
||||
{
|
||||
#ifdef PLATFORM_WINDOWS
|
||||
if (_map_addr) {
|
||||
UnmapViewOfFile (_map_addr);
|
||||
}
|
||||
#else
|
||||
munmap ((void*) _map_addr, _map_length);
|
||||
#endif
|
||||
close (_fd);
|
||||
_map_addr = 0;
|
||||
}
|
||||
|
||||
int
|
||||
Mp3FileImportableSource::decode_mp3 ()
|
||||
{
|
||||
_pcm_off = 0;
|
||||
do {
|
||||
_n_frames = mp3dec_decode_frame (&_mp3d, _buffer, _remain, _pcm, &_info);
|
||||
_buffer += _info.frame_bytes;
|
||||
_remain -= _info.frame_bytes;
|
||||
if (_n_frames) {
|
||||
break;
|
||||
}
|
||||
} while (_info.frame_bytes);
|
||||
return _n_frames;
|
||||
}
|
||||
|
||||
void
|
||||
Mp3FileImportableSource::seek (samplepos_t pos)
|
||||
{
|
||||
if (_read_position == pos) {
|
||||
return;
|
||||
}
|
||||
|
||||
/* rewind, then decode to pos */
|
||||
if (pos < _read_position) {
|
||||
_buffer = _map_addr;
|
||||
_remain = _map_length;
|
||||
_read_position = 0;
|
||||
_pcm_off = 0;
|
||||
mp3dec_init (&_mp3d);
|
||||
decode_mp3 ();
|
||||
}
|
||||
|
||||
while (_read_position + _n_frames <= pos) {
|
||||
if (!decode_mp3 ()) {
|
||||
break;
|
||||
}
|
||||
_read_position += _n_frames;
|
||||
}
|
||||
|
||||
if (_n_frames >= 0) {
|
||||
_pcm_off = _info.channels * (pos - _read_position);
|
||||
_n_frames -= pos - _read_position;
|
||||
_read_position = pos;
|
||||
}
|
||||
assert (_pcm_off >= 0 && _pcm_off < MINIMP3_MAX_SAMPLES_PER_FRAME);
|
||||
}
|
||||
|
||||
samplecnt_t
|
||||
Mp3FileImportableSource::read (Sample* dst, samplecnt_t nframes)
|
||||
{
|
||||
size_t dst_off = 0;
|
||||
int remain = nframes; // == samples * channels
|
||||
while (remain > 0) {
|
||||
samplecnt_t samples_to_copy = std::min (remain, _n_frames * _info.channels);
|
||||
if (samples_to_copy > 0) {
|
||||
memcpy (&dst[dst_off], &_pcm[_pcm_off], samples_to_copy * sizeof (Sample));
|
||||
remain -= samples_to_copy;
|
||||
dst_off += samples_to_copy;
|
||||
_n_frames -= samples_to_copy / _info.channels;
|
||||
_pcm_off += samples_to_copy;
|
||||
_read_position += samples_to_copy / _info.channels;
|
||||
}
|
||||
assert (_n_frames >= 0);
|
||||
if (_n_frames <= 0 && !decode_mp3 ()) {
|
||||
/* EOF, or decode error */
|
||||
break;
|
||||
}
|
||||
}
|
||||
return dst_off;
|
||||
}
|
||||
|
||||
samplecnt_t
|
||||
Mp3FileImportableSource::read_unlocked (Sample* dst, samplepos_t start, samplecnt_t cnt, uint32_t chn)
|
||||
{
|
||||
const uint32_t n_chn = channels ();
|
||||
if (chn > n_chn || cnt == 0) {
|
||||
return 0;
|
||||
}
|
||||
if (start != _read_position) {
|
||||
seek (start);
|
||||
}
|
||||
|
||||
size_t dst_off = 0;
|
||||
samplecnt_t remain = cnt;
|
||||
|
||||
while (remain > 0) {
|
||||
samplecnt_t samples_to_copy = std::min (remain, (samplecnt_t)_n_frames);
|
||||
|
||||
for (samplecnt_t n = 0; n < samples_to_copy; ++n) {
|
||||
dst[dst_off] = _pcm[_pcm_off + chn];
|
||||
dst_off += 1;
|
||||
remain -= 1;
|
||||
_n_frames -= 1;
|
||||
_pcm_off += n_chn;
|
||||
_read_position += 1;
|
||||
}
|
||||
|
||||
assert (_n_frames >= 0);
|
||||
if (_n_frames <= 0 && !decode_mp3 ()) {
|
||||
/* EOF, or decode error */
|
||||
break;
|
||||
}
|
||||
}
|
||||
return dst_off;
|
||||
}
|
|
@ -0,0 +1,77 @@
|
|||
/*
|
||||
* Copyright (C) 2019 Robin Gareus <robin@gareus.org>
|
||||
*
|
||||
* This program is free software; you can redistribute it and/or modify
|
||||
* it under the terms of the GNU General Public License as published by
|
||||
* the Free Software Foundation; either version 2 of the License, or
|
||||
* (at your option) any later version.
|
||||
*
|
||||
* This program is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
* GNU General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU General Public License along
|
||||
* with this program; if not, write to the Free Software Foundation, Inc.,
|
||||
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
|
||||
*/
|
||||
|
||||
#include "pbd/error.h"
|
||||
#include "pbd/compose.h"
|
||||
#include "ardour/mp3filesource.h"
|
||||
|
||||
#include "pbd/i18n.h"
|
||||
|
||||
using namespace ARDOUR;
|
||||
using namespace PBD;
|
||||
|
||||
/** Constructor to be called for existing external-to-session files
|
||||
* Sources created with this method are never writable or removable.
|
||||
*/
|
||||
|
||||
Mp3FileSource::Mp3FileSource (Session& s, const string& path, int chn, Flag flags)
|
||||
: Source (s, DataType::AUDIO, path,
|
||||
Source::Flag (flags & ~(Writable|Removable|RemovableIfEmpty|RemoveAtDestroy)))
|
||||
, AudioFileSource (s, path,
|
||||
Source::Flag (flags & ~(Writable|Removable|RemovableIfEmpty|RemoveAtDestroy)))
|
||||
, _mp3 (path)
|
||||
, _channel (chn)
|
||||
{
|
||||
_length = _mp3.length ();
|
||||
|
||||
if (_channel >= (int) _mp3.channels ()) {
|
||||
error << string_compose("Mp3FileSource: file only contains %1 channels; %2 is invalid as a channel number (%3)", _mp3.channels (), _channel, name()) << endmsg;
|
||||
throw failed_constructor();
|
||||
}
|
||||
}
|
||||
|
||||
Mp3FileSource::~Mp3FileSource ()
|
||||
{
|
||||
}
|
||||
|
||||
void
|
||||
Mp3FileSource::close ()
|
||||
{
|
||||
}
|
||||
|
||||
samplecnt_t
|
||||
Mp3FileSource::read_unlocked (Sample* dst, samplepos_t start, samplecnt_t cnt) const
|
||||
{
|
||||
return _mp3.read_unlocked (dst, start, cnt, _channel);
|
||||
}
|
||||
|
||||
int
|
||||
Mp3FileSource::get_soundfile_info (string path, SoundFileInfo& _info, string& error_msg)
|
||||
{
|
||||
try {
|
||||
Mp3FileImportableSource mp3 (path);
|
||||
_info.samplerate = mp3.samplerate ();
|
||||
_info.channels = mp3.channels ();
|
||||
_info.length = mp3.length ();
|
||||
_info.format_name = string_compose (_("MPEG Layer %1 (%2 kbps)"), mp3.layer (), mp3.bitrate ());
|
||||
_info.timecode = 0;
|
||||
_info.seekable = false;
|
||||
return 0;
|
||||
} catch (...) {}
|
||||
return -1;
|
||||
}
|
|
@ -34,6 +34,7 @@
|
|||
#include "ardour/boost_debug.h"
|
||||
#include "ardour/midi_playlist.h"
|
||||
#include "ardour/midi_playlist_source.h"
|
||||
#include "ardour/mp3filesource.h"
|
||||
#include "ardour/source.h"
|
||||
#include "ardour/source_factory.h"
|
||||
#include "ardour/sndfilesource.h"
|
||||
|
@ -281,6 +282,19 @@ SourceFactory::createExternal (DataType type, Session& s, const string& path,
|
|||
} catch (...) { }
|
||||
#endif
|
||||
|
||||
/* only create mp3s for audition: no announce, no peaks */
|
||||
if (!announce && (!AudioFileSource::get_build_peakfiles () || defer_peaks)) {
|
||||
try {
|
||||
Source* src = new Mp3FileSource (s, path, chn, flags);
|
||||
#ifdef BOOST_SP_ENABLE_DEBUG_HOOKS
|
||||
// boost_debug_shared_ptr_mark_interesting (src, "Source");
|
||||
#endif
|
||||
boost::shared_ptr<Source> ret (src);
|
||||
return ret;
|
||||
|
||||
} catch (failed_constructor& err) { }
|
||||
}
|
||||
|
||||
} else {
|
||||
// eh?
|
||||
}
|
||||
|
|
|
@ -148,6 +148,8 @@ libardour_sources = [
|
|||
'mode.cc',
|
||||
'monitor_control.cc',
|
||||
'monitor_processor.cc',
|
||||
'mp3fileimportable.cc',
|
||||
'mp3filesource.cc',
|
||||
'mtc_slave.cc',
|
||||
'mtdm.cc',
|
||||
'muteable.cc',
|
||||
|
|
Loading…
Reference in New Issue