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.

222 lines
6.8KB

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