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.

230 lines
7.1KB

  1. /*
  2. ==============================================================================
  3. This file is part of the JUCE library.
  4. Copyright (c) 2017 - ROLI Ltd.
  5. JUCE is an open source library subject to commercial or open-source
  6. licensing.
  7. By using JUCE, you agree to the terms of both the JUCE 5 End-User License
  8. Agreement and JUCE 5 Privacy Policy (both updated and effective as of the
  9. 27th April 2017).
  10. End User License Agreement: www.juce.com/juce-5-licence
  11. Privacy Policy: www.juce.com/juce-5-privacy-policy
  12. Or: You may also use this code under the terms of the GPL v3 (see
  13. www.gnu.org/licenses).
  14. JUCE IS PROVIDED "AS IS" WITHOUT ANY WARRANTY, AND ALL WARRANTIES, WHETHER
  15. EXPRESSED OR IMPLIED, INCLUDING MERCHANTABILITY AND FITNESS FOR PURPOSE, ARE
  16. DISCLAIMED.
  17. ==============================================================================
  18. */
  19. namespace juce
  20. {
  21. #if JUCE_USE_LAME_AUDIO_FORMAT
  22. class LAMEEncoderAudioFormat::Writer : public AudioFormatWriter
  23. {
  24. public:
  25. Writer (OutputStream* destStream, const String& formatName,
  26. const File& appFile, int vbr, int cbr,
  27. double sampleRate, unsigned int numberOfChannels,
  28. int bitsPerSample, const StringPairArray& metadata)
  29. : AudioFormatWriter (destStream, formatName, sampleRate,
  30. numberOfChannels, (unsigned int) bitsPerSample),
  31. vbrLevel (vbr), cbrBitrate (cbr)
  32. {
  33. WavAudioFormat wavFormat;
  34. if (auto* out = tempWav.getFile().createOutputStream())
  35. {
  36. writer.reset (wavFormat.createWriterFor (out, sampleRate, numChannels,
  37. bitsPerSample, metadata, 0));
  38. args.add (appFile.getFullPathName());
  39. args.add ("--quiet");
  40. if (cbrBitrate == 0)
  41. {
  42. args.add ("--vbr-new");
  43. args.add ("-V");
  44. args.add (String (vbrLevel));
  45. }
  46. else
  47. {
  48. args.add ("--cbr");
  49. args.add ("-b");
  50. args.add (String (cbrBitrate));
  51. }
  52. addMetadataArg (metadata, "id3title", "--tt");
  53. addMetadataArg (metadata, "id3artist", "--ta");
  54. addMetadataArg (metadata, "id3album", "--tl");
  55. addMetadataArg (metadata, "id3comment", "--tc");
  56. addMetadataArg (metadata, "id3date", "--ty");
  57. addMetadataArg (metadata, "id3genre", "--tg");
  58. addMetadataArg (metadata, "id3trackNumber", "--tn");
  59. }
  60. }
  61. void addMetadataArg (const StringPairArray& metadata, const char* key, const char* lameFlag)
  62. {
  63. auto value = metadata.getValue (key, {});
  64. if (value.isNotEmpty())
  65. {
  66. args.add (lameFlag);
  67. args.add (value);
  68. }
  69. }
  70. ~Writer()
  71. {
  72. if (writer != nullptr)
  73. {
  74. writer = nullptr;
  75. if (! convertToMP3())
  76. convertToMP3(); // try again
  77. }
  78. }
  79. bool write (const int** samplesToWrite, int numSamples)
  80. {
  81. return writer != nullptr && writer->write (samplesToWrite, numSamples);
  82. }
  83. private:
  84. int vbrLevel, cbrBitrate;
  85. TemporaryFile tempWav { ".wav" };
  86. std::unique_ptr<AudioFormatWriter> writer;
  87. StringArray args;
  88. bool runLameChildProcess (const TemporaryFile& tempMP3, const StringArray& processArgs) const
  89. {
  90. ChildProcess cp;
  91. if (cp.start (processArgs))
  92. {
  93. auto childOutput = cp.readAllProcessOutput();
  94. DBG (childOutput); ignoreUnused (childOutput);
  95. cp.waitForProcessToFinish (10000);
  96. return tempMP3.getFile().getSize() > 0;
  97. }
  98. return false;
  99. }
  100. bool convertToMP3() const
  101. {
  102. TemporaryFile tempMP3 (".mp3");
  103. StringArray args2 (args);
  104. args2.add (tempWav.getFile().getFullPathName());
  105. args2.add (tempMP3.getFile().getFullPathName());
  106. DBG (args2.joinIntoString (" "));
  107. if (runLameChildProcess (tempMP3, args2))
  108. {
  109. FileInputStream fis (tempMP3.getFile());
  110. if (fis.openedOk() && output->writeFromInputStream (fis, -1) > 0)
  111. {
  112. output->flush();
  113. return true;
  114. }
  115. }
  116. return false;
  117. }
  118. JUCE_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR (Writer)
  119. };
  120. //==============================================================================
  121. LAMEEncoderAudioFormat::LAMEEncoderAudioFormat (const File& lameApplication)
  122. : AudioFormat ("MP3 file", ".mp3"),
  123. lameApp (lameApplication)
  124. {
  125. }
  126. LAMEEncoderAudioFormat::~LAMEEncoderAudioFormat()
  127. {
  128. }
  129. bool LAMEEncoderAudioFormat::canHandleFile (const File&)
  130. {
  131. return false;
  132. }
  133. Array<int> LAMEEncoderAudioFormat::getPossibleSampleRates()
  134. {
  135. return { 32000, 44100, 48000 };
  136. }
  137. Array<int> LAMEEncoderAudioFormat::getPossibleBitDepths()
  138. {
  139. return { 16 };
  140. }
  141. bool LAMEEncoderAudioFormat::canDoStereo() { return true; }
  142. bool LAMEEncoderAudioFormat::canDoMono() { return true; }
  143. bool LAMEEncoderAudioFormat::isCompressed() { return true; }
  144. StringArray LAMEEncoderAudioFormat::getQualityOptions()
  145. {
  146. static const char* vbrOptions[] = { "VBR quality 0 (best)", "VBR quality 1", "VBR quality 2", "VBR quality 3",
  147. "VBR quality 4 (normal)", "VBR quality 5", "VBR quality 6", "VBR quality 7",
  148. "VBR quality 8", "VBR quality 9 (smallest)", nullptr };
  149. StringArray opts (vbrOptions);
  150. const int cbrRates[] = { 32, 40, 48, 56, 64, 80, 96, 112, 128, 160, 192, 224, 256, 320 };
  151. for (int i = 0; i < numElementsInArray (cbrRates); ++i)
  152. opts.add (String (cbrRates[i]) + " Kb/s CBR");
  153. return opts;
  154. }
  155. AudioFormatReader* LAMEEncoderAudioFormat::createReaderFor (InputStream*, const bool)
  156. {
  157. return nullptr;
  158. }
  159. AudioFormatWriter* LAMEEncoderAudioFormat::createWriterFor (OutputStream* streamToWriteTo,
  160. double sampleRateToUse,
  161. unsigned int numberOfChannels,
  162. int bitsPerSample,
  163. const StringPairArray& metadataValues,
  164. int qualityOptionIndex)
  165. {
  166. if (streamToWriteTo == nullptr)
  167. return nullptr;
  168. int vbr = 4;
  169. int cbr = 0;
  170. const String qual (getQualityOptions() [qualityOptionIndex]);
  171. if (qual.contains ("VBR"))
  172. vbr = qual.retainCharacters ("0123456789").getIntValue();
  173. else
  174. cbr = qual.getIntValue();
  175. return new Writer (streamToWriteTo, getFormatName(), lameApp, vbr, cbr,
  176. sampleRateToUse, numberOfChannels, bitsPerSample, metadataValues);
  177. }
  178. #endif
  179. } // namespace juce