MythTV master
lameencoder.cpp
Go to the documentation of this file.
1/*
2 MP3 encoding support using liblame for MythMusic
3
4 (c) 2003 Stefan Frank
5
6 Please send an e-mail to sfr@gmx.net if you have
7 questions or comments.
8
9 Project Website: out http://www.mythtv.org/
10
11 This program is free software; you can redistribute it and/or modify
12 it under the terms of the GNU General Public License as published by
13 the Free Software Foundation; either version 2 of the License, or
14 (at your option) any later version.
15
16 This program is distributed in the hope that it will be useful,
17 but WITHOUT ANY WARRANTY; without even the implied warranty of
18 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 GNU General Public License for more details.
20
21 You should have received a copy of the GNU General Public License
22 along with this program; if not, write to the Free Software
23 Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
24*/
25
26// c++
27#include <iostream>
28
29// qt
30#include <QApplication>
31#include <QString>
32
33// MythTV
37
38// mythmusic
39#include "lameencoder.h"
40
41static int write_buffer(char *buf, int bufsize, FILE *fp)
42{
43 return fwrite(buf, 1, bufsize, fp);
44}
45
46void LameEncoder::init_id3tags(lame_global_flags *gf)
47{
48 // Very basic ID3v2 Header. Rewritten later, but libid3tag.
49 id3tag_init(gf);
50
51 // Dummy tag. It'll get overwritten later by the more flexible
52 // libid3 (MAD). Unf. it wont write the id3 tag to the file if
53 // none exists, hense this dummy entry.
54 id3tag_set_title(gf, "Title");
55
56 // write v2 tags.
57 id3tag_v2_only(gf);
58}
59
60int LameEncoder::init_encoder(lame_global_flags *gf, int quality, bool vbr) const
61{
62 int lameret = 0;
63 int meanbitrate = 128;
64 int preset = STANDARD;
65
66 switch (quality)
67 {
68 case 0: // low, always use CBR
69 meanbitrate = 128;
70 vbr = false;
71 break;
72 case 1: // medium
73 meanbitrate = 192;
74 break;
75 case 2: // high
76 meanbitrate = 256;
77 preset = EXTREME;
78 break;
79 }
80
81 if (vbr)
82 {
83 lame_set_preset(gf, preset);
84 }
85 else
86 {
87 lame_set_preset(gf, meanbitrate);
88 lame_set_VBR(gf, vbr_off);
89 }
90
91 if (m_channels == 1)
92 {
93 lame_set_mode(gf, MONO);
94 }
95
96 lameret = lame_init_params(gf);
97
98 return lameret;
99}
100
101LameEncoder::LameEncoder(const QString &outfile, int qualitylevel,
102 MusicMetadata *metadata, bool vbr) :
103 Encoder(outfile, qualitylevel, metadata),
104 m_mp3Buf(new char[m_mp3BufSize]),
105 m_gf(lame_init())
106{
108
109 int lameret = init_encoder(m_gf, qualitylevel, vbr);
110 if (lameret < 0)
111 {
112 LOG(VB_GENERAL, LOG_ERR,
113 QString("Error initializing LAME encoder. Got return code: %1")
114 .arg(lameret));
115 return;
116 }
117}
118
120{
121 LameEncoder::addSamples(nullptr, 0); //flush
122
123 if (m_gf && m_out)
124 lame_mp3_tags_fid (m_gf, m_out);
125 if (m_gf)
126 lame_close(m_gf);
127 delete[] m_mp3Buf;
128
129 // Need to close the file here.
130 if (m_out)
131 {
132 fclose(m_out);
133
134 // Make sure the base class doesn't do a double clear.
135 m_out = nullptr;
136 }
137
138 // Now write the Metadata
139 if (m_metadata)
141}
142
143int LameEncoder::addSamples(int16_t * bytes, unsigned int length)
144{
145 int lameret = 0;
146
148
149 if (length > 0)
150 {
151 lameret = lame_encode_buffer_interleaved(m_gf, bytes,
153 (unsigned char *)m_mp3Buf,
155 }
156 else
157 {
158 lameret = lame_encode_flush(m_gf, (unsigned char *)m_mp3Buf,
160 }
161
162 if (lameret < 0)
163 {
164 LOG(VB_GENERAL, LOG_ERR, QString("LAME encoder error."));
165 }
166 else if (lameret > 0 && m_out)
167 {
168 if (write_buffer(m_mp3Buf, lameret, m_out) != lameret)
169 {
170 LOG(VB_GENERAL, LOG_ERR, "Failed to write mp3 data. Aborting.");
171 return EENCODEERROR;
172 }
173 }
174
175 return 0;
176}
177
FILE * m_out
Definition: encoder.h:26
MusicMetadata * m_metadata
Definition: encoder.h:28
const QString m_outfile
Definition: encoder.h:25
int m_mp3BufSize
Definition: lameencoder.h:55
char * m_mp3Buf
Definition: lameencoder.h:56
LameEncoder(const QString &outfile, int qualitylevel, MusicMetadata *metadata, bool vbr=false)
int m_channels
Definition: lameencoder.h:50
~LameEncoder() override
static void init_id3tags(lame_global_flags *gf)
Definition: lameencoder.cpp:46
lame_global_flags * m_gf
Definition: lameencoder.h:58
int m_samplesPerChannel
Definition: lameencoder.h:52
int addSamples(int16_t *bytes, unsigned int len) override
int m_bytesPerSample
Definition: lameencoder.h:51
int init_encoder(lame_global_flags *gf, int quality, bool vbr) const
Definition: lameencoder.cpp:60
Read and write metadata in MPEG (mp3) ID3V2 tags.
Definition: metaioid3.h:39
bool write(const QString &filename, MusicMetadata *mdata) override
Writes all metadata back to a file.
Definition: metaioid3.cpp:153
@ EENCODEERROR
Definition: encoder.h:8
static int write_buffer(char *buf, int bufsize, FILE *fp)
Definition: lameencoder.cpp:41
#define LOG(_MASK_, _LEVEL_, _QSTRING_)
Definition: mythlogging.h:39
int FILE
Definition: mythburn.py:137