MuseScore  3.4
Music composition and notation
exportmp3.h
Go to the documentation of this file.
1 //=============================================================================
2 // MuseScore
3 // Linux Music Score Editor
4 //
5 // Copyright (C) 2011 Werner Schweer and others
6 //
7 // This program is free software; you can redistribute it and/or modify
8 // it under the terms of the GNU General Public License version 2.
9 //
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.
14 //
15 // You should have received a copy of the GNU General Public License
16 // along with this program; if not, write to the Free Software
17 // Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
18 //=============================================================================
19 
20 #ifndef __EXPORTMP3_H__
21 #define __EXPORTMP3_H__
22 
23 #include "globals.h"
24 #include "lame/lame.h"
25 #if defined(Q_OS_WIN)
26 #include "windows.h"
27 #endif
28 
29 namespace Ms {
30 
31 //----------------------------------------------------------------------------
32 // ExportMP3Options
33 //----------------------------------------------------------------------------
34 
35 #define MODE_SET 0
36 #define MODE_VBR 1
37 #define MODE_ABR 2
38 #define MODE_CBR 3
39 
40 #define CHANNEL_JOINT 0
41 #define CHANNEL_STEREO 1
42 
43 #define QUALITY_0 0
44 #define QUALITY_1 1
45 #define QUALITY_2 2
46 #define QUALITY_3 3
47 #define QUALITY_4 4
48 #define QUALITY_5 5
49 #define QUALITY_6 6
50 #define QUALITY_7 7
51 #define QUALITY_8 8
52 #define QUALITY_9 9
53 
54 #define ROUTINE_FAST 0
55 #define ROUTINE_STANDARD 1
56 
57 #define PRESET_INSANE 0
58 #define PRESET_EXTREME 1
59 #define PRESET_STANDARD 2
60 #define PRESET_MEDIUM 3
61 
62 
63 //----------------------------------------------------------------------------
64 // MP3Exporter
65 //----------------------------------------------------------------------------
66 
67 typedef lame_global_flags *lame_init_t(void);
68 typedef int lame_init_params_t(lame_global_flags*);
69 typedef const char* get_lame_version_t(void);
70 
72  lame_global_flags* gf,
73  const float buffer_l [],
74  const float buffer_r [],
75  const int nsamples,
76  unsigned char * mp3buf,
77  const int mp3buf_size );
78 
79 typedef int lame_encode_flush_t(
80  lame_global_flags *gf,
81  unsigned char* mp3buf,
82  int size );
83 
84 typedef int lame_close_t(lame_global_flags*);
85 
86 typedef int lame_set_in_samplerate_t(lame_global_flags*, int);
87 typedef int lame_set_out_samplerate_t(lame_global_flags*, int);
88 typedef int lame_set_num_channels_t(lame_global_flags*, int );
89 typedef int lame_set_quality_t(lame_global_flags*, int);
90 typedef int lame_set_brate_t(lame_global_flags*, int);
91 typedef int lame_set_VBR_t(lame_global_flags *, vbr_mode);
92 typedef int lame_set_VBR_q_t(lame_global_flags *, int);
93 typedef int lame_set_VBR_min_bitrate_kbps_t(lame_global_flags *, int);
94 typedef int lame_set_mode_t(lame_global_flags *, MPEG_mode);
95 typedef int lame_set_preset_t(lame_global_flags *, int);
96 typedef int lame_set_error_protection_t(lame_global_flags *, int);
97 typedef int lame_set_disable_reservoir_t(lame_global_flags *, int);
98 typedef int lame_set_padding_type_t(lame_global_flags *, Padding_type);
99 typedef int lame_set_bWriteVbrTag_t(lame_global_flags *, int);
100 typedef size_t lame_get_lametag_frame_t(const lame_global_flags *, unsigned char* buffer, size_t size);
101 typedef void lame_mp3_tags_fid_t(lame_global_flags *, FILE *);
102 
103 #if defined(Q_OS_WIN)
104 // An alternative solution to give Windows an additional chance of writing the tag before
105 // falling bato to lame_mp3_tag_fid(). The latter can have DLL sharing issues when mixing
106 // Debug/Release builds of Audacity and the lame DLL.
107 typedef unsigned long beWriteInfoTag_t(lame_global_flags *, char *);
108 
109 // We use this to determine if the user has selected an older, Blade API only, lame_enc.dll
110 // so we can be more specific about why their library isn't acceptable.
111 typedef struct {
112 
113  // BladeEnc DLL Version number
114 
115  BYTE byDLLMajorVersion;
116  BYTE byDLLMinorVersion;
117 
118  // BladeEnc Engine Version Number
119 
120  BYTE byMajorVersion;
121  BYTE byMinorVersion;
122 
123  // DLL Release date
124 
125  BYTE byDay;
126  BYTE byMonth;
127  WORD wYear;
128 
129  // BladeEnc Homepage URL
130 
131  CHAR zHomepage[129];
132 
133  BYTE byAlphaLevel;
134  BYTE byBetaLevel;
135  BYTE byMMXEnabled;
136 
137  BYTE btReserved[125];
138 } be_version;
139 typedef void beVersion_t(be_version *);
140 #endif
141 
142 //---------------------------------------------------------
143 // MP3Exporter
144 //---------------------------------------------------------
145 
146 class MP3Exporter {
147 
148  public:
149  enum class AskUser : char { NO, MAYBE, YES };
150 
151  MP3Exporter();
152  virtual ~MP3Exporter();
153 
154  bool findLibrary();
155  bool loadLibrary(AskUser askuser);
156  bool validLibraryLoaded();
157 
158  /* These global settings keep state over the life of the object */
159  void setMode(int mode);
160  void setBitrate(int rate);
161  void setQuality(int q, int r);
162  void setChannel(int mode);
163 
164  /* Virtual methods that must be supplied by library interfaces */
165 
166  /* initialize the library interface */
167  bool initLibrary(QString libpath);
168  void freeLibrary();
169 
170  /* get library info */
171  QString getLibraryVersion();
172  QString getLibraryName();
173  QString getLibraryPath();
174  QString getLibraryTypeString();
175 
176  /* returns the number of samples PER CHANNEL to send for each call to EncodeBuffer */
177  int initializeStream(int channels, int sampleRate);
178 
179  /* In bytes. must be called AFTER InitializeStream */
180  int getOutBufferSize();
181 
182  /* returns the number of bytes written. input is separate per channel */
183  void bufferPreamp(float buffer[], int nSamples);
184  int encodeBuffer(float inbufferL[], float inbufferR[], unsigned char outbuffer[]);
185  int encodeRemainder(float inbufferL[], float inbufferR[], int nSamples,
186  unsigned char outbuffer[]);
187 
188  int encodeBufferMono(float inbuffer[], unsigned char outbuffer[]);
189  int encodeRemainderMono(float inbuffer[], int nSamples,
190  unsigned char outbuffer[]);
191 
192  int finishStream(unsigned char outbuffer[]);
193  void cancelEncoding();
194 
195 // void PutInfoTag(QFile f, qint64 off);
196 
197  private:
198  QString mLibPath;
199  QLibrary* lame_lib;
201 
202 #if defined(Q_OS_WIN)
203  QString mBladeVersion;
204 #endif
205 
206  bool mEncoding;
207  int mMode;
208  int mBitrate;
209  int mQuality;
210  int mRoutine;
211  int mChannel;
212 
213  /* function pointers to the symbols we get from the library */
220 
237 #if defined(Q_OS_WIN)
238  beWriteInfoTag_t *beWriteInfoTag;
239  beVersion_t *beVersion;
240 #endif
241 
242  lame_global_flags *mGF;
243 
244  static const int mSamplesPerChunk = 220500;
245  // See lame.h/lame_encode_buffer() for further explanation
246  // As coded here, this should be the worst case.
247  static const int mOutBufferSize =
248  mSamplesPerChunk * (320 / 8) / 8 + 4 * 1152 * (320 / 8) / 8 + 512;
249 
250  // See MAXFRAMESIZE in libmp3lame/VbrTag.c for explanation of 2880.
251  unsigned char mInfoTagBuf[2880];
252  size_t mInfoTagLen;
253  };
254 
255 
256 } // namespace Ms
257 #endif //__EXPORTMP3_H__
MP3Exporter()
Definition: exportmp3.cpp:34
lame_encode_buffer_float_t * lame_encode_buffer_float
Definition: exportmp3.h:216
int encodeBufferMono(float inbuffer[], unsigned char outbuffer[])
Definition: exportmp3.cpp:470
int lame_encode_flush_t(lame_global_flags *gf, unsigned char *mp3buf, int size)
Definition: exportmp3.h:79
bool mEncoding
Definition: exportmp3.h:206
lame_set_VBR_t * lame_set_VBR
Definition: exportmp3.h:226
lame_set_preset_t * lame_set_preset
Definition: exportmp3.h:230
lame_close_t * lame_close
Definition: exportmp3.h:218
int encodeRemainder(float inbufferL[], float inbufferR[], int nSamples, unsigned char outbuffer[])
Definition: exportmp3.cpp:454
int lame_set_error_protection_t(lame_global_flags *, int)
Definition: exportmp3.h:96
bool findLibrary()
Definition: exportmp3.cpp:59
void lame_mp3_tags_fid_t(lame_global_flags *, FILE *)
Definition: exportmp3.h:101
void setChannel(int mode)
Definition: exportmp3.cpp:178
int getOutBufferSize()
Definition: exportmp3.cpp:417
virtual ~MP3Exporter()
Definition: exportmp3.cpp:50
void bufferPreamp(float buffer[], int nSamples)
Definition: exportmp3.cpp:429
int lame_set_mode_t(lame_global_flags *, MPEG_mode)
Definition: exportmp3.h:94
lame_init_t * lame_init
Definition: exportmp3.h:214
void setQuality(int q, int r)
Definition: exportmp3.cpp:172
QString getLibraryName()
Definition: exportmp3.cpp:596
int lame_set_VBR_min_bitrate_kbps_t(lame_global_flags *, int)
Definition: exportmp3.h:93
size_t mInfoTagLen
Definition: exportmp3.h:252
bool mLibraryLoaded
Definition: exportmp3.h:200
int lame_set_quality_t(lame_global_flags *, int)
Definition: exportmp3.h:89
lame_global_flags * lame_init_t(void)
Definition: exportmp3.h:67
lame_set_padding_type_t * lame_set_padding_type
Definition: exportmp3.h:233
int lame_set_brate_t(lame_global_flags *, int)
Definition: exportmp3.h:90
int lame_encode_buffer_float_t(lame_global_flags *gf, const float buffer_l [], const float buffer_r [], const int nsamples, unsigned char *mp3buf, const int mp3buf_size)
Definition: exportmp3.h:71
lame_set_VBR_min_bitrate_kbps_t * lame_set_VBR_min_bitrate_kbps
Definition: exportmp3.h:228
QLibrary * lame_lib
Definition: exportmp3.h:199
AskUser
Definition: exportmp3.h:149
lame_set_num_channels_t * lame_set_num_channels
Definition: exportmp3.h:223
lame_init_params_t * lame_init_params
Definition: exportmp3.h:215
lame_encode_flush_t * lame_encode_flush
Definition: exportmp3.h:217
lame_set_bWriteVbrTag_t * lame_set_bWriteVbrTag
Definition: exportmp3.h:234
int lame_set_num_channels_t(lame_global_flags *, int)
Definition: exportmp3.h:88
lame_mp3_tags_fid_t * lame_mp3_tags_fid
Definition: exportmp3.h:236
lame_get_lametag_frame_t * lame_get_lametag_frame
Definition: exportmp3.h:235
get_lame_version_t * get_lame_version
Definition: exportmp3.h:219
int lame_set_preset_t(lame_global_flags *, int)
Definition: exportmp3.h:95
int mBitrate
Definition: exportmp3.h:208
QString getLibraryVersion()
Definition: exportmp3.cpp:316
int lame_init_params_t(lame_global_flags *)
Definition: exportmp3.h:68
int lame_set_out_samplerate_t(lame_global_flags *, int)
Definition: exportmp3.h:87
lame_set_disable_reservoir_t * lame_set_disable_reservoir
Definition: exportmp3.h:232
Definition: aeolus.cpp:26
void setBitrate(int rate)
Definition: exportmp3.cpp:167
lame_set_error_protection_t * lame_set_error_protection
Definition: exportmp3.h:231
int mRoutine
Definition: exportmp3.h:210
void setMode(int mode)
Definition: exportmp3.cpp:162
QString getLibraryTypeString()
Definition: exportmp3.cpp:601
int finishStream(unsigned char outbuffer[])
Definition: exportmp3.cpp:499
bool loadLibrary(AskUser askuser)
Definition: exportmp3.cpp:104
int mQuality
Definition: exportmp3.h:209
int mChannel
Definition: exportmp3.h:211
lame_set_VBR_q_t * lame_set_VBR_q
Definition: exportmp3.h:227
int lame_set_bWriteVbrTag_t(lame_global_flags *, int)
Definition: exportmp3.h:99
Definition: exportmp3.h:146
int lame_set_disable_reservoir_t(lame_global_flags *, int)
Definition: exportmp3.h:97
size_t lame_get_lametag_frame_t(const lame_global_flags *, unsigned char *buffer, size_t size)
Definition: exportmp3.h:100
static const int mSamplesPerChunk
Definition: exportmp3.h:244
int initializeStream(int channels, int sampleRate)
Definition: exportmp3.cpp:327
int lame_set_VBR_q_t(lame_global_flags *, int)
Definition: exportmp3.h:92
bool validLibraryLoaded()
Definition: exportmp3.cpp:157
lame_set_quality_t * lame_set_quality
Definition: exportmp3.h:224
int lame_set_in_samplerate_t(lame_global_flags *, int)
Definition: exportmp3.h:86
lame_global_flags * mGF
Definition: exportmp3.h:242
int lame_set_VBR_t(lame_global_flags *, vbr_mode)
Definition: exportmp3.h:91
lame_set_in_samplerate_t * lame_set_in_samplerate
Definition: exportmp3.h:221
lame_set_out_samplerate_t * lame_set_out_samplerate
Definition: exportmp3.h:222
int encodeRemainderMono(float inbuffer[], int nSamples, unsigned char outbuffer[])
Definition: exportmp3.cpp:484
int lame_set_padding_type_t(lame_global_flags *, Padding_type)
Definition: exportmp3.h:98
int lame_close_t(lame_global_flags *)
Definition: exportmp3.h:84
void freeLibrary()
Definition: exportmp3.cpp:301
int mMode
Definition: exportmp3.h:207
QString mLibPath
Definition: exportmp3.h:198
const char * get_lame_version_t(void)
Definition: exportmp3.h:69
lame_set_mode_t * lame_set_mode
Definition: exportmp3.h:229
bool initLibrary(QString libpath)
Definition: exportmp3.cpp:187
QString getLibraryPath()
Q_OS_MAC.
Definition: exportmp3.cpp:591
static const int mOutBufferSize
Definition: exportmp3.h:247
unsigned char mInfoTagBuf[2880]
Definition: exportmp3.h:251
void cancelEncoding()
Definition: exportmp3.cpp:516
int encodeBuffer(float inbufferL[], float inbufferR[], unsigned char outbuffer[])
Definition: exportmp3.cpp:439
lame_set_brate_t * lame_set_brate
Definition: exportmp3.h:225