/
AudioFileMP3.cpp
134 lines (110 loc) · 3.68 KB
/
AudioFileMP3.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
/*
* AudioFileMP3.cpp - Audio-device which encodes a wave stream into
* an MP3 file. This is used for song export.
*
* Copyright (c) 2017 to present Michael Gregorius <michael.gregorius.git/at/arcor[dot]de>
*
* This file is part of LMMS - https://lmms.io
*
* 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 (see COPYING); if not, write to the
* Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
* Boston, MA 02110-1301 USA.
*
*/
#include "AudioFileMP3.h"
#ifdef LMMS_HAVE_MP3LAME
#include "Mixer.h"
#include <cassert>
AudioFileMP3::AudioFileMP3( OutputSettings const & outputSettings,
const ch_cnt_t channels,
bool & successful,
const QString & file,
Mixer* mixer ) :
AudioFileDevice( outputSettings, channels, file, mixer )
{
successful = true;
// For now only accept stereo sources
successful &= channels == 2;
successful &= initEncoder();
successful &= outputFileOpened();
}
AudioFileMP3::~AudioFileMP3()
{
flushRemainingBuffers();
tearDownEncoder();
}
void AudioFileMP3::writeBuffer( const surroundSampleFrame * _buf,
const fpp_t _frames,
const float _master_gain )
{
if (_frames < 1)
{
return;
}
// TODO Why isn't the gain applied by the driver but inside the device?
std::vector<float> interleavedDataBuffer(_frames * 2);
for (fpp_t i = 0; i < _frames; ++i)
{
interleavedDataBuffer[2*i] = _buf[i][0] * _master_gain;
interleavedDataBuffer[2*i + 1] = _buf[i][1] * _master_gain;
}
size_t minimumBufferSize = 1.25 * _frames + 7200;
std::vector<unsigned char> encodingBuffer(minimumBufferSize);
int bytesWritten = lame_encode_buffer_interleaved_ieee_float(m_lame, &interleavedDataBuffer[0], _frames, &encodingBuffer[0], static_cast<int>(encodingBuffer.size()));
assert (bytesWritten >= 0);
writeData(&encodingBuffer[0], bytesWritten);
}
void AudioFileMP3::flushRemainingBuffers()
{
// The documentation states that flush should have at least 7200 bytes. So let's be generous.
std::vector<unsigned char> encodingBuffer(7200 * 4);
int bytesWritten = lame_encode_flush(m_lame, &encodingBuffer[0], static_cast<int>(encodingBuffer.size()));
assert (bytesWritten >= 0);
writeData(&encodingBuffer[0], bytesWritten);
}
MPEG_mode mapToMPEG_mode(OutputSettings::StereoMode stereoMode)
{
switch (stereoMode)
{
case OutputSettings::StereoMode_Stereo:
return STEREO;
case OutputSettings::StereoMode_JointStereo:
return JOINT_STEREO;
case OutputSettings::StereoMode_Mono:
return MONO;
default:
return NOT_SET;
}
}
bool AudioFileMP3::initEncoder()
{
m_lame = lame_init();
// Handle stereo/joint/mono settings
OutputSettings::StereoMode stereoMode = getOutputSettings().getStereoMode();
lame_set_mode(m_lame, mapToMPEG_mode(stereoMode));
// Handle bit rate settings
OutputSettings::BitRateSettings bitRateSettings = getOutputSettings().getBitRateSettings();
int bitRate = static_cast<int>(bitRateSettings.getBitRate());
lame_set_VBR(m_lame, vbr_off);
lame_set_brate(m_lame, bitRate);
// Add a comment
id3tag_init(m_lame);
id3tag_set_comment(m_lame, "Created with LMMS");
return lame_init_params(m_lame) != -1;
}
void AudioFileMP3::tearDownEncoder()
{
lame_close(m_lame);
}
#endif