2 * Copyright (C) 2005-2013 Team XBMC
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, or (at your option)
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
16 * along with XBMC; see the file COPYING. If not, see
17 * <http://www.gnu.org/licenses/>.
21 #include "EncoderLame.h"
22 #include "settings/Settings.h"
23 #include "utils/log.h"
26 extern "C" FILE *fopen_utf8(const char *_Filename, const char *_Mode);
28 #define fopen_utf8 fopen
31 CEncoderLame::CEncoderLame()
33 memset(m_inPath, 0, 1024 + 1);
34 memset(m_outPath, 0, 1024 + 1);
37 bool CEncoderLame::Init(const char* strFile, int iInChannels, int iInRate, int iInBits)
39 // we only accept 2 / 44100 / 16 atm
40 if (iInChannels != 2 || iInRate != 44100 || iInBits != 16) return false;
42 // set input stream information and open the file
43 if (!CEncoder::Init(strFile, iInChannels, iInRate, iInBits)) return false;
49 m_pGlobalFlags = m_dll.lame_init();
52 CLog::Log(LOGERROR, "Error: lame_init() failed");
56 // setup parmaters, see lame.h for possibilities
57 if (CSettings::Get().GetInt("audiocds.quality") == CDDARIP_QUALITY_CBR)
59 int bitrate = CSettings::Get().GetInt("audiocds.bitrate");
60 // use cbr and specified bitrate from settings
61 CLog::Log(LOGDEBUG, "Lame setting CBR bitrate %d", bitrate);
62 m_dll.lame_set_brate(m_pGlobalFlags, bitrate);
68 switch (CSettings::Get().GetInt("audiocds.quality"))
70 case CDDARIP_QUALITY_MEDIUM:
73 case CDDARIP_QUALITY_STANDARD:
76 case CDDARIP_QUALITY_EXTREME:
82 CLog::Log(LOGDEBUG, "Lame setting preset %d", preset);
83 m_dll.lame_set_preset(m_pGlobalFlags, preset);
86 m_dll.lame_set_asm_optimizations(m_pGlobalFlags, MMX, 1);
87 m_dll.lame_set_asm_optimizations(m_pGlobalFlags, SSE, 1);
88 m_dll.lame_set_in_samplerate(m_pGlobalFlags, 44100);
90 // Setup the ID3 tagger
91 m_dll.id3tag_init(m_pGlobalFlags);
92 m_dll.id3tag_add_v2(m_pGlobalFlags);
93 m_dll.id3tag_set_title(m_pGlobalFlags, m_strTitle.c_str());
94 m_dll.id3tag_set_artist(m_pGlobalFlags, m_strArtist.c_str());
95 m_dll.id3tag_set_textinfo_latin1(m_pGlobalFlags, "TPE2", m_strAlbumArtist.c_str());
96 m_dll.id3tag_set_album(m_pGlobalFlags, m_strAlbum.c_str());
97 m_dll.id3tag_set_year(m_pGlobalFlags, m_strYear.c_str());
98 m_dll.id3tag_set_track(m_pGlobalFlags, m_strTrack.c_str());
99 int test = m_dll.id3tag_set_genre(m_pGlobalFlags, m_strGenre.c_str());
101 m_dll.id3tag_set_genre(m_pGlobalFlags,"Other");
103 // Now that all the options are set, lame needs to analyze them and
104 // set some more internal options and check for problems
105 if (m_dll.lame_init_params(m_pGlobalFlags) < 0)
107 CLog::Log(LOGERROR, "Error: Cannot init Lame params");
114 int CEncoderLame::Encode(int nNumBytesRead, BYTE* pbtStream)
116 int iBytes = m_dll.lame_encode_buffer_interleaved(m_pGlobalFlags, (short*)pbtStream, nNumBytesRead / 4, m_buffer, sizeof(m_buffer));
120 CLog::Log(LOGERROR, "Internal Lame error: %i", iBytes);
124 if (WriteStream(m_buffer, iBytes) != iBytes)
126 CLog::Log(LOGERROR, "Error writing Lame buffer to file");
133 bool CEncoderLame::Close()
135 // may return one more mp3 frames
136 int iBytes = m_dll.lame_encode_flush(m_pGlobalFlags, m_buffer, sizeof(m_buffer));
140 CLog::Log(LOGERROR, "Internal Lame error: %i", iBytes);
144 WriteStream(m_buffer, iBytes);
148 // open again, but now the old way, lame only accepts FILE pointers
149 FILE* file = fopen_utf8(m_strFile.c_str(), "rb+");
152 CLog::Log(LOGERROR, "Error: Cannot open file for writing tags: %s", m_strFile.c_str());
156 m_dll.lame_mp3_tags_fid(m_pGlobalFlags, file); /* add VBR tags to mp3 file */
159 m_dll.lame_close(m_pGlobalFlags);
161 // unload the lame dll