The JUCE cross-platform C++ framework, with DISTRHO/KXStudio specific changes
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

220 lines
7.1KB

  1. /*
  2. ==============================================================================
  3. This file is part of the JUCE library.
  4. Copyright (c) 2013 - Raw Material Software Ltd.
  5. Permission is granted to use this software under the terms of either:
  6. a) the GPL v2 (or any later version)
  7. b) the Affero GPL v3
  8. Details of these licenses can be found at: www.gnu.org/licenses
  9. JUCE is distributed in the hope that it will be useful, but WITHOUT ANY
  10. WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
  11. A PARTICULAR PURPOSE. See the GNU General Public License for more details.
  12. ------------------------------------------------------------------------------
  13. To release a closed-source product which uses JUCE, commercial licenses are
  14. available: visit www.juce.com for more information.
  15. ==============================================================================
  16. */
  17. #if JUCE_USE_LAME_AUDIO_FORMAT
  18. class LAMEEncoderAudioFormat::Writer : public AudioFormatWriter
  19. {
  20. public:
  21. Writer (OutputStream* destStream, const String& formatName,
  22. const File& lameApp, int vbr, int cbr,
  23. double sampleRate, unsigned int numberOfChannels,
  24. unsigned int bitsPerSample, const StringPairArray& metadata)
  25. : AudioFormatWriter (destStream, formatName, sampleRate,
  26. numberOfChannels, bitsPerSample),
  27. vbrLevel (vbr), cbrBitrate (cbr),
  28. tempWav (".wav")
  29. {
  30. WavAudioFormat wavFormat;
  31. if (FileOutputStream* out = tempWav.getFile().createOutputStream())
  32. {
  33. writer = wavFormat.createWriterFor (out, sampleRate, numChannels,
  34. bitsPerSample, metadata, 0);
  35. args.add (lameApp.getFullPathName());
  36. args.add ("--quiet");
  37. if (cbrBitrate == 0)
  38. {
  39. args.add ("-vbr-new");
  40. args.add ("-V");
  41. args.add (String (vbrLevel));
  42. }
  43. else
  44. {
  45. args.add ("--cbr");
  46. args.add ("-b");
  47. args.add (String (cbrBitrate));
  48. }
  49. addMetadataArg (metadata, "id3title", "--tt");
  50. addMetadataArg (metadata, "id3artist", "--ta");
  51. addMetadataArg (metadata, "id3album", "--tl");
  52. addMetadataArg (metadata, "id3comment", "--tc");
  53. addMetadataArg (metadata, "id3date", "--ty");
  54. addMetadataArg (metadata, "id3genre", "--tg");
  55. addMetadataArg (metadata, "id3trackNumber", "--tn");
  56. }
  57. }
  58. void addMetadataArg (const StringPairArray& metadata, const char* key, const char* lameFlag)
  59. {
  60. const String value (metadata.getValue (key, String::empty));
  61. if (value.isNotEmpty())
  62. {
  63. args.add (lameFlag);
  64. args.add (value);
  65. }
  66. }
  67. ~Writer()
  68. {
  69. if (writer != nullptr)
  70. {
  71. writer = nullptr;
  72. if (! convertToMP3())
  73. convertToMP3(); // try again
  74. }
  75. }
  76. bool write (const int** samplesToWrite, int numSamples)
  77. {
  78. return writer != nullptr && writer->write (samplesToWrite, numSamples);
  79. }
  80. private:
  81. int vbrLevel, cbrBitrate;
  82. TemporaryFile tempWav;
  83. ScopedPointer<AudioFormatWriter> writer;
  84. StringArray args;
  85. bool convertToMP3() const
  86. {
  87. TemporaryFile tempMP3 (".mp3");
  88. StringArray args2 (args);
  89. args2.add (tempWav.getFile().getFullPathName());
  90. args2.add (tempMP3.getFile().getFullPathName());
  91. ChildProcess cp;
  92. DBG (args2.joinIntoString(" "));
  93. if (cp.start (args2))
  94. {
  95. String childOutput (cp.readAllProcessOutput());
  96. DBG (childOutput);
  97. if (tempMP3.getFile().getSize() > 0)
  98. {
  99. FileInputStream fis (tempMP3.getFile());
  100. if (fis.openedOk() && output->writeFromInputStream (fis, -1) > 0)
  101. {
  102. output->flush();
  103. return true;
  104. }
  105. }
  106. }
  107. return false;
  108. }
  109. JUCE_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR (Writer)
  110. };
  111. //==============================================================================
  112. static const char* const lameFormatName = "MP3 file";
  113. static const char* const lameExtensions[] = { ".mp3", nullptr };
  114. LAMEEncoderAudioFormat::LAMEEncoderAudioFormat (const File& lameApplication)
  115. : AudioFormat (TRANS (lameFormatName), StringArray (lameExtensions)),
  116. lameApp (lameApplication)
  117. {
  118. }
  119. LAMEEncoderAudioFormat::~LAMEEncoderAudioFormat()
  120. {
  121. }
  122. bool LAMEEncoderAudioFormat::canHandleFile (const File&)
  123. {
  124. return false;
  125. }
  126. Array<int> LAMEEncoderAudioFormat::getPossibleSampleRates()
  127. {
  128. const int rates[] = { 32000, 44100, 48000, 0 };
  129. return Array <int> (rates);
  130. }
  131. Array<int> LAMEEncoderAudioFormat::getPossibleBitDepths()
  132. {
  133. const int depths[] = { 16, 0 };
  134. return Array <int> (depths);
  135. }
  136. bool LAMEEncoderAudioFormat::canDoStereo() { return true; }
  137. bool LAMEEncoderAudioFormat::canDoMono() { return true; }
  138. bool LAMEEncoderAudioFormat::isCompressed() { return true; }
  139. StringArray LAMEEncoderAudioFormat::getQualityOptions()
  140. {
  141. const char* vbrOptions[] = { "VBR quality 0 (best)", "VBR quality 1", "VBR quality 2", "VBR quality 3",
  142. "VBR quality 4 (normal)", "VBR quality 5", "VBR quality 6", "VBR quality 7", "VBR quality 8",
  143. "VBR quality 9 (smallest)",
  144. nullptr };
  145. StringArray opts (vbrOptions);
  146. const int cbrRates[] = { 32, 40, 48, 56, 64, 80, 96, 112, 128, 160, 192, 224, 256, 320 };
  147. for (int i = 0; i < numElementsInArray (cbrRates); ++i)
  148. opts.add (String (cbrRates[i]) + " Kb/s CBR");
  149. return opts;
  150. }
  151. AudioFormatReader* LAMEEncoderAudioFormat::createReaderFor (InputStream*, const bool)
  152. {
  153. return nullptr;
  154. }
  155. AudioFormatWriter* LAMEEncoderAudioFormat::createWriterFor (OutputStream* streamToWriteTo,
  156. double sampleRateToUse,
  157. unsigned int numberOfChannels,
  158. int bitsPerSample,
  159. const StringPairArray& metadataValues,
  160. int qualityOptionIndex)
  161. {
  162. int vbr = 4;
  163. int cbr = 0;
  164. const String qual (getQualityOptions() [qualityOptionIndex]);
  165. if (qual.contains ("VBR"))
  166. vbr = qual.retainCharacters ("0123456789").getIntValue();
  167. else
  168. cbr = qual.getIntValue();
  169. return new Writer (streamToWriteTo, getFormatName(), lameApp, vbr, cbr,
  170. sampleRateToUse, numberOfChannels, bitsPerSample, metadataValues);
  171. }
  172. #endif