📄 exportmp3.cpp
字号:
/********************************************************************** Audacity: A Digital Audio Editor ExportMP3.cpp Joshua Haberman This just acts as an interface to LAME. A Lame dynamic library must be present The difficulty in our approach is that we are attempting to use LAME in a way it was not designed to be used. LAME's API is reasonably consistant, so if we were linking directly against it we could expect this code to work with a variety of different LAME versions. However, the data structures change from version to version, and so linking with one version of the header and dynamically linking against a different version of the dynamic library will not work correctly. The solution is to find the lowest common denominator between versions. The bare minimum of functionality we must use is this: 1. Initialize the library. 2. Set, at minimum, the following global options: i. input sample rate ii. input channels 3. Encode the stream 4. Call the finishing routine Just so that it's clear that we're NOT free to use whatever features of LAME we like, I'm not including lame.h, but instead enumerating here the extent of functions and structures that we can rely on being able to import and use from a dynamic library. For the record, we aim to support LAME 3.70 on. Since LAME 3.70 was released in April of 2000, that should be plenty. Copyright 2002, 2003 Joshua Haberman. Some portions may be Copyright 2003 Paolo Patruno. 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., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA**********************************************************************/#include <wx/defs.h>#include <wx/textctrl.h>#include <wx/dynlib.h>#include <wx/msgdlg.h>#include <wx/utils.h>#include <wx/progdlg.h>#include <wx/timer.h>#include <wx/window.h>#include <wx/ffile.h>#include <wx/log.h>#include <wx/filedlg.h>#include <wx/intl.h>#include "../Audacity.h"#include "ExportMP3.h"#include "../Internat.h"#include "../Mix.h"#include "../Prefs.h"#include "../Project.h"#include "../Tags.h"#include "../WaveTrack.h"#ifdef __WXMAC__#define __MOVIES__ /* Apple's Movies.h not compatible with Audacity *//* #define __MACHELP__ */#include <wx/mac/private.h># ifdef __UNIX__# include <CoreServices/CoreServices.h># else# include <Files.h># endif#endifMP3Exporter::MP3Exporter(){ if (gPrefs) mLibPath = gPrefs->Read(wxT("/MP3/MP3LibPath"), wxT(""));}bool MP3Exporter::FindLibrary(wxWindow *parent){ mLibPath = gPrefs->Read(wxT("/MP3/MP3LibPath"), wxT("")); if (mLibPath==wxT("") || !::wxFileExists(FILENAME(mLibPath))) { int action = wxMessageBox(GetLibraryMessage(), _("Export MP3"), wxYES_NO | wxICON_EXCLAMATION, parent); if (action == wxYES) { wxString question; /* i18n-hint: It's asking for the location of a file, for example, "Where is lame_enc.dll?" - you could translate "Where would I find the file %s" instead if you want. */ question.Printf(_("Where is %s?"), GetLibraryName().c_str()); mLibPath = wxFileSelector(question, GetLibraryPath(), // Path GetLibraryName(), // Name wxT(""), // Extension GetLibraryTypeString(), wxOPEN, parent); if (mLibPath == wxT("")) { mLibPath = wxT(""); gPrefs->Write(wxT("/MP3/MP3LibPath"), mLibPath); return false; } wxString path, baseName, extension; ::wxSplitPath(mLibPath, &path, &baseName, &extension); if (extension != wxT("")) baseName += wxT(".") + extension; if (baseName.CmpNoCase(GetLibraryName())) { wxString question; question.Printf(_("Audacity was expecting a library named \"%s\". Are you sure you want to attempt to export MP3 files using \"%s\"?"), GetLibraryName().c_str(), baseName.c_str()); int action = wxMessageBox(question, _("Export MP3"), wxYES_NO | wxICON_EXCLAMATION, parent); if (action != wxYES) { mLibPath = wxT(""); gPrefs->Write(wxT("/MP3/MP3LibPath"), mLibPath); return false; } } } else { mLibPath = wxT(""); gPrefs->Write(wxT("/MP3/MP3LibPath"), mLibPath); return false; } gPrefs->Write(wxT("/MP3/MP3LibPath"), mLibPath); } return true;}#if defined(__WXGTK__) || defined(__WXX11__) || defined(__WXMOTIF__) /* --------------------------------------------------------------------------*/ struct lame_global_flags; typedef lame_global_flags *lame_init_t(void); typedef int lame_init_params_t(lame_global_flags*); typedef const char* get_lame_version_t(void); typedef int lame_encode_buffer_t ( lame_global_flags* gf, const short int buffer_l [], const short int buffer_r [], const int nsamples, unsigned char * mp3buf, const int mp3buf_size ); typedef int lame_encode_buffer_interleaved_t( lame_global_flags* gf, short int pcm[], int num_samples, /* per channel */ unsigned char* mp3buf, int mp3buf_size ); typedef int lame_encode_flush_t( lame_global_flags *gf, unsigned char* mp3buf, int size ); typedef int lame_close_t(lame_global_flags*); typedef int lame_set_in_samplerate_t(lame_global_flags*, int); typedef int lame_set_num_channels_t(lame_global_flags*, int ); typedef int lame_set_quality_t(lame_global_flags*, int); typedef int lame_get_quality_t(lame_global_flags*); typedef int lame_set_brate_t(lame_global_flags*, int); typedef int lame_get_brate_t(lame_global_flags*); /* --------------------------------------------------------------------------*/ class LinuxLAMEExporter : public MP3Exporter { private: /* function pointers to the symbols we get from the library */ lame_init_t* lame_init; lame_init_params_t* lame_init_params; lame_encode_buffer_t* lame_encode_buffer; lame_encode_buffer_interleaved_t* lame_encode_buffer_interleaved; lame_encode_flush_t* lame_encode_flush; lame_close_t* lame_close; get_lame_version_t* get_lame_version; lame_set_in_samplerate_t* lame_set_in_samplerate; lame_set_num_channels_t* lame_set_num_channels; lame_set_quality_t* lame_set_quality; lame_get_quality_t* lame_get_quality; lame_set_brate_t* lame_set_brate; lame_get_brate_t* lame_get_brate; lame_global_flags *mGF; bool mLibraryLoaded, mEncoding; char mVersion[20]; static const int mSamplesPerChunk = 220500; static const int mOutBufferSize = int(1.25 * mSamplesPerChunk + 7200); public: LinuxLAMEExporter() { mLibraryLoaded = false; mEncoding = false; mGF = NULL; } wxString GetLibraryPath() { return wxT("/usr/lib"); } wxString GetLibraryName() { return wxT("libmp3lame.so"); } wxString GetLibraryTypeString() { return wxString(_("Only libmp3lame.so|libmp3lame.so|Primary Shared Object files (*.so)|*.so|Extended Libraries (*.so*)|*.so*|All Files (*)|*")); } wxString GetLibraryMessage() { /* i18n-hint: This message is used on Unix/Linux */ return _("Audacity does not export MP3 files directly, but instead uses the \n" "freely available LAME library to handle MP3 file encoding. You must \n" "obtain libmp3lame.so separately, either by downloading it or building \n" "it from the sources, and then locate the file for Audacity. You only \n" "need to do this once.\n\n" "Would you like to locate libmp3lame.so now?"); } bool LoadLibrary() { wxLogNull logNo; //BG: I was unable to test the wxDynamicLibrary code on this platform if (wxFileExists(FILENAME(mLibPath))) { if(lame_enc_lib.IsLoaded()) { lame_enc_lib.Unload(); } if(!lame_enc_lib.Load(FILENAME(mLibPath))) { return false; } } else return false; /* get function pointers from the shared library */ lame_init = (lame_init_t *)lame_enc_lib.GetSymbol(wxT("lame_init")); get_lame_version = (get_lame_version_t *)lame_enc_lib.GetSymbol(wxT("get_lame_version")); lame_init_params = (lame_init_params_t *) lame_enc_lib.GetSymbol(wxT("lame_init_params")); lame_encode_buffer = (lame_encode_buffer_t *) lame_enc_lib.GetSymbol(wxT("lame_encode_buffer")); lame_encode_buffer_interleaved = (lame_encode_buffer_interleaved_t *) lame_enc_lib.GetSymbol(wxT("lame_encode_buffer_interleaved")); lame_encode_flush = (lame_encode_flush_t *) lame_enc_lib.GetSymbol(wxT("lame_encode_flush")); lame_close = (lame_close_t *) lame_enc_lib.GetSymbol(wxT("lame_close")); lame_close = (lame_close_t *) lame_enc_lib.GetSymbol(wxT("lame_close")); lame_set_in_samplerate = (lame_set_in_samplerate_t *) lame_enc_lib.GetSymbol(wxT("lame_set_in_samplerate")); lame_set_num_channels = (lame_set_num_channels_t *) lame_enc_lib.GetSymbol(wxT("lame_set_num_channels")); lame_set_quality = (lame_set_quality_t *) lame_enc_lib.GetSymbol(wxT("lame_set_quality")); lame_get_quality = (lame_get_quality_t *) lame_enc_lib.GetSymbol(wxT("lame_get_quality")); lame_set_brate = (lame_set_brate_t *) lame_enc_lib.GetSymbol(wxT("lame_set_brate")); lame_get_brate = (lame_get_brate_t *) lame_enc_lib.GetSymbol(wxT("lame_get_brate")); /* we assume that if all the symbols are found, it's a valid library */ if (!lame_init || !get_lame_version || !lame_init_params || !lame_encode_buffer || !lame_encode_buffer_interleaved || !lame_encode_flush || !lame_close || !lame_set_in_samplerate || !lame_set_num_channels || !lame_set_quality || !lame_set_brate) { return false; } mGF = lame_init(); mLibraryLoaded = true; return true; } bool ValidLibraryLoaded() { return mLibraryLoaded; } wxString GetLibraryVersion() { if(!mLibraryLoaded) return wxT(""); return wxString::Format(wxT("LAME %hs"), get_lame_version()); } int InitializeStream(int channels, int sampleRate) { if(!mLibraryLoaded) return -1; lame_set_num_channels(mGF, channels); lame_set_in_samplerate(mGF, sampleRate); lame_init_params(mGF); mEncoding = true; return mSamplesPerChunk; } int GetOutBufferSize() { return mOutBufferSize; } int EncodeBuffer(short int inbuffer[], unsigned char outbuffer[]) { if(!mEncoding) return -1; return lame_encode_buffer_interleaved(mGF, inbuffer, mSamplesPerChunk, outbuffer, mOutBufferSize); } int EncodeRemainder(short int inbuffer[], int nSamples, unsigned char outbuffer[]) { return lame_encode_buffer_interleaved(mGF, inbuffer, nSamples, outbuffer, mOutBufferSize);
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -