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.

228 lines
7.2KB

  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. #if JUCE_USE_LAME_AUDIO_FORMAT
  20. class LAMEEncoderAudioFormat::Writer : public AudioFormatWriter
  21. {
  22. public:
  23. Writer (OutputStream* destStream, const String& formatName,
  24. const File& appFile, int vbr, int cbr,
  25. double sampleRate, unsigned int numberOfChannels,
  26. int bitsPerSample, const StringPairArray& metadata)
  27. : AudioFormatWriter (destStream, formatName, sampleRate,
  28. numberOfChannels, (unsigned int) bitsPerSample),
  29. vbrLevel (vbr), cbrBitrate (cbr),
  30. tempWav (".wav")
  31. {
  32. WavAudioFormat wavFormat;
  33. if (FileOutputStream* out = tempWav.getFile().createOutputStream())
  34. {
  35. writer = wavFormat.createWriterFor (out, sampleRate, numChannels,
  36. bitsPerSample, metadata, 0);
  37. args.add (appFile.getFullPathName());
  38. args.add ("--quiet");
  39. if (cbrBitrate == 0)
  40. {
  41. args.add ("--vbr-new");
  42. args.add ("-V");
  43. args.add (String (vbrLevel));
  44. }
  45. else
  46. {
  47. args.add ("--cbr");
  48. args.add ("-b");
  49. args.add (String (cbrBitrate));
  50. }
  51. addMetadataArg (metadata, "id3title", "--tt");
  52. addMetadataArg (metadata, "id3artist", "--ta");
  53. addMetadataArg (metadata, "id3album", "--tl");
  54. addMetadataArg (metadata, "id3comment", "--tc");
  55. addMetadataArg (metadata, "id3date", "--ty");
  56. addMetadataArg (metadata, "id3genre", "--tg");
  57. addMetadataArg (metadata, "id3trackNumber", "--tn");
  58. }
  59. }
  60. void addMetadataArg (const StringPairArray& metadata, const char* key, const char* lameFlag)
  61. {
  62. const String value (metadata.getValue (key, String()));
  63. if (value.isNotEmpty())
  64. {
  65. args.add (lameFlag);
  66. args.add (value);
  67. }
  68. }
  69. ~Writer()
  70. {
  71. if (writer != nullptr)
  72. {
  73. writer = nullptr;
  74. if (! convertToMP3())
  75. convertToMP3(); // try again
  76. }
  77. }
  78. bool write (const int** samplesToWrite, int numSamples)
  79. {
  80. return writer != nullptr && writer->write (samplesToWrite, numSamples);
  81. }
  82. private:
  83. int vbrLevel, cbrBitrate;
  84. TemporaryFile tempWav;
  85. ScopedPointer<AudioFormatWriter> writer;
  86. StringArray args;
  87. bool runLameChildProcess (const TemporaryFile& tempMP3, const StringArray& processArgs) const
  88. {
  89. ChildProcess cp;
  90. if (cp.start (processArgs))
  91. {
  92. const String childOutput (cp.readAllProcessOutput());
  93. DBG (childOutput); ignoreUnused (childOutput);
  94. cp.waitForProcessToFinish (10000);
  95. return tempMP3.getFile().getSize() > 0;
  96. }
  97. return false;
  98. }
  99. bool convertToMP3() const
  100. {
  101. TemporaryFile tempMP3 (".mp3");
  102. StringArray args2 (args);
  103. args2.add (tempWav.getFile().getFullPathName());
  104. args2.add (tempMP3.getFile().getFullPathName());
  105. DBG (args2.joinIntoString (" "));
  106. if (runLameChildProcess (tempMP3, args2))
  107. {
  108. FileInputStream fis (tempMP3.getFile());
  109. if (fis.openedOk() && output->writeFromInputStream (fis, -1) > 0)
  110. {
  111. output->flush();
  112. return true;
  113. }
  114. }
  115. return false;
  116. }
  117. JUCE_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR (Writer)
  118. };
  119. //==============================================================================
  120. LAMEEncoderAudioFormat::LAMEEncoderAudioFormat (const File& lameApplication)
  121. : AudioFormat ("MP3 file", ".mp3"),
  122. lameApp (lameApplication)
  123. {
  124. }
  125. LAMEEncoderAudioFormat::~LAMEEncoderAudioFormat()
  126. {
  127. }
  128. bool LAMEEncoderAudioFormat::canHandleFile (const File&)
  129. {
  130. return false;
  131. }
  132. Array<int> LAMEEncoderAudioFormat::getPossibleSampleRates()
  133. {
  134. const int rates[] = { 32000, 44100, 48000, 0 };
  135. return Array<int> (rates);
  136. }
  137. Array<int> LAMEEncoderAudioFormat::getPossibleBitDepths()
  138. {
  139. const int depths[] = { 16, 0 };
  140. return Array<int> (depths);
  141. }
  142. bool LAMEEncoderAudioFormat::canDoStereo() { return true; }
  143. bool LAMEEncoderAudioFormat::canDoMono() { return true; }
  144. bool LAMEEncoderAudioFormat::isCompressed() { return true; }
  145. StringArray LAMEEncoderAudioFormat::getQualityOptions()
  146. {
  147. static const char* vbrOptions[] = { "VBR quality 0 (best)", "VBR quality 1", "VBR quality 2", "VBR quality 3",
  148. "VBR quality 4 (normal)", "VBR quality 5", "VBR quality 6", "VBR quality 7",
  149. "VBR quality 8", "VBR quality 9 (smallest)", nullptr };
  150. StringArray opts (vbrOptions);
  151. const int cbrRates[] = { 32, 40, 48, 56, 64, 80, 96, 112, 128, 160, 192, 224, 256, 320 };
  152. for (int i = 0; i < numElementsInArray (cbrRates); ++i)
  153. opts.add (String (cbrRates[i]) + " Kb/s CBR");
  154. return opts;
  155. }
  156. AudioFormatReader* LAMEEncoderAudioFormat::createReaderFor (InputStream*, const bool)
  157. {
  158. return nullptr;
  159. }
  160. AudioFormatWriter* LAMEEncoderAudioFormat::createWriterFor (OutputStream* streamToWriteTo,
  161. double sampleRateToUse,
  162. unsigned int numberOfChannels,
  163. int bitsPerSample,
  164. const StringPairArray& metadataValues,
  165. int qualityOptionIndex)
  166. {
  167. if (streamToWriteTo == nullptr)
  168. return nullptr;
  169. int vbr = 4;
  170. int cbr = 0;
  171. const String qual (getQualityOptions() [qualityOptionIndex]);
  172. if (qual.contains ("VBR"))
  173. vbr = qual.retainCharacters ("0123456789").getIntValue();
  174. else
  175. cbr = qual.getIntValue();
  176. return new Writer (streamToWriteTo, getFormatName(), lameApp, vbr, cbr,
  177. sampleRateToUse, numberOfChannels, bitsPerSample, metadataValues);
  178. }
  179. #endif