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.

337 lines
12KB

  1. /*
  2. ==============================================================================
  3. This file is part of the JUCE library - "Jules' Utility Class Extensions"
  4. Copyright 2004-11 by Raw Material Software Ltd.
  5. ------------------------------------------------------------------------------
  6. JUCE can be redistributed and/or modified under the terms of the GNU General
  7. Public License (Version 2), as published by the Free Software Foundation.
  8. A copy of the license is included in the JUCE distribution, or can be found
  9. online at www.gnu.org/licenses.
  10. JUCE is distributed in the hope that it will be useful, but WITHOUT ANY
  11. WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
  12. A PARTICULAR PURPOSE. See the GNU General Public License for more details.
  13. ------------------------------------------------------------------------------
  14. To release a closed-source product which uses JUCE, commercial licenses are
  15. available: visit www.rawmaterialsoftware.com/juce for more information.
  16. ==============================================================================
  17. */
  18. #if JUCE_WINDOWS
  19. namespace WindowsMediaCodec
  20. {
  21. class JuceIStream : public ComBaseClassHelper <IStream>
  22. {
  23. public:
  24. JuceIStream (InputStream& source_) noexcept
  25. : source (source_)
  26. {
  27. resetReferenceCount();
  28. }
  29. JUCE_COMRESULT Commit (DWORD) { return S_OK; }
  30. JUCE_COMRESULT Write (const void*, ULONG, ULONG*) { return E_NOTIMPL; }
  31. JUCE_COMRESULT Clone (IStream**) { return E_NOTIMPL; }
  32. JUCE_COMRESULT SetSize (ULARGE_INTEGER) { return E_NOTIMPL; }
  33. JUCE_COMRESULT Revert() { return E_NOTIMPL; }
  34. JUCE_COMRESULT LockRegion (ULARGE_INTEGER, ULARGE_INTEGER, DWORD) { return E_NOTIMPL; }
  35. JUCE_COMRESULT UnlockRegion (ULARGE_INTEGER, ULARGE_INTEGER, DWORD) { return E_NOTIMPL; }
  36. JUCE_COMRESULT Read (void* dest, ULONG numBytes, ULONG* bytesRead)
  37. {
  38. const int numRead = source.read (dest, numBytes);
  39. if (bytesRead != nullptr)
  40. *bytesRead = numRead;
  41. return numRead == (int) numBytes ? S_OK : S_FALSE;
  42. }
  43. JUCE_COMRESULT Seek (LARGE_INTEGER position, DWORD origin, ULARGE_INTEGER* resultPosition)
  44. {
  45. int64 newPos = (int64) position.QuadPart;
  46. if (origin == STREAM_SEEK_CUR)
  47. {
  48. newPos += source.getPosition();
  49. }
  50. else if (origin == STREAM_SEEK_END)
  51. {
  52. const int64 len = source.getTotalLength();
  53. if (len < 0)
  54. return E_NOTIMPL;
  55. newPos += len;
  56. }
  57. if (resultPosition != nullptr)
  58. resultPosition->QuadPart = newPos;
  59. return source.setPosition (newPos) ? S_OK : E_NOTIMPL;
  60. }
  61. JUCE_COMRESULT CopyTo (IStream* destStream, ULARGE_INTEGER numBytesToDo,
  62. ULARGE_INTEGER* bytesRead, ULARGE_INTEGER* bytesWritten)
  63. {
  64. uint64 totalCopied = 0;
  65. int64 numBytes = numBytesToDo.QuadPart;
  66. while (numBytes > 0 && ! source.isExhausted())
  67. {
  68. char buffer [1024];
  69. const int numToCopy = (int) jmin ((int64) sizeof (buffer), (int64) numBytes);
  70. const int numRead = source.read (buffer, numToCopy);
  71. if (numRead <= 0)
  72. break;
  73. destStream->Write (buffer, numRead, nullptr);
  74. totalCopied += numRead;
  75. }
  76. if (bytesRead != nullptr) bytesRead->QuadPart = totalCopied;
  77. if (bytesWritten != nullptr) bytesWritten->QuadPart = totalCopied;
  78. return S_OK;
  79. }
  80. JUCE_COMRESULT Stat (STATSTG* stat, DWORD)
  81. {
  82. if (stat == nullptr)
  83. return STG_E_INVALIDPOINTER;
  84. zerostruct (*stat);
  85. stat->type = STGTY_STREAM;
  86. stat->cbSize.QuadPart = jmax ((int64) 0, source.getTotalLength());
  87. return S_OK;
  88. }
  89. private:
  90. InputStream& source;
  91. JUCE_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR (JuceIStream);
  92. };
  93. //==============================================================================
  94. static const char* wmFormatName = "Windows Media";
  95. static const char* const extensions[] = { ".mp3", ".wmv", ".asf", 0 };
  96. //==============================================================================
  97. class WMAudioReader : public AudioFormatReader
  98. {
  99. public:
  100. WMAudioReader (InputStream* const input_)
  101. : AudioFormatReader (input_, TRANS (wmFormatName)),
  102. ok (false),
  103. wmvCoreLib ("Wmvcore.dll"),
  104. currentPosition (0),
  105. bufferStart (0), bufferEnd (0)
  106. {
  107. typedef HRESULT (*WMCreateSyncReaderType) (IUnknown*, DWORD, IWMSyncReader**);
  108. WMCreateSyncReaderType wmCreateSyncReader = nullptr;
  109. wmCreateSyncReader = (WMCreateSyncReaderType) wmvCoreLib.getFunction ("WMCreateSyncReader");
  110. if (wmCreateSyncReader != nullptr)
  111. {
  112. HRESULT hr = wmCreateSyncReader (nullptr, WMT_RIGHT_PLAYBACK, wmSyncReader.resetAndGetPointerAddress());
  113. hr = wmSyncReader->OpenStream (new JuceIStream (*input));
  114. if (SUCCEEDED (hr))
  115. {
  116. WORD streamNum = 1;
  117. hr = wmSyncReader->GetStreamNumberForOutput (0, &streamNum);
  118. hr = wmSyncReader->SetReadStreamSamples (streamNum, false);
  119. scanFileForDetails();
  120. ok = sampleRate > 0;
  121. }
  122. }
  123. }
  124. ~WMAudioReader()
  125. {
  126. if (wmSyncReader != nullptr)
  127. {
  128. wmSyncReader->Close();
  129. wmSyncReader = nullptr;
  130. }
  131. }
  132. bool readSamples (int** destSamples, int numDestChannels, int startOffsetInDestBuffer,
  133. int64 startSampleInFile, int numSamples)
  134. {
  135. if (! ok)
  136. return false;
  137. if (startSampleInFile != currentPosition)
  138. {
  139. currentPosition = startSampleInFile;
  140. wmSyncReader->SetRange (((QWORD) startSampleInFile * 10000000) / sampleRate, 0);
  141. bufferStart = bufferEnd = 0;
  142. }
  143. while (numSamples > 0)
  144. {
  145. if (bufferEnd <= bufferStart)
  146. {
  147. INSSBuffer* sampleBuffer = nullptr;
  148. QWORD sampleTime, duration;
  149. DWORD flags, outputNum;
  150. WORD streamNum;
  151. HRESULT hr = wmSyncReader->GetNextSample (0, &sampleBuffer, &sampleTime,
  152. &duration, &flags, &outputNum, &streamNum);
  153. if (SUCCEEDED (hr))
  154. {
  155. BYTE* rawData = nullptr;
  156. DWORD dataLength = 0;
  157. hr = sampleBuffer->GetBufferAndLength (&rawData, &dataLength);
  158. jassert (SUCCEEDED (hr));
  159. bufferStart = 0;
  160. bufferEnd = (int) dataLength;
  161. if (bufferEnd <= 0)
  162. return false;
  163. buffer.ensureSize (bufferEnd);
  164. memcpy (buffer.getData(), rawData, bufferEnd);
  165. }
  166. else
  167. {
  168. bufferStart = 0;
  169. bufferEnd = 512;
  170. buffer.ensureSize (bufferEnd);
  171. buffer.fillWith (0);
  172. }
  173. }
  174. const int stride = numChannels * sizeof (int16);
  175. const int16* const rawData = static_cast <const int16*> (addBytesToPointer (buffer.getData(), bufferStart));
  176. const int numToDo = jmin (numSamples, (bufferEnd - bufferStart) / stride);
  177. for (int i = 0; i < numDestChannels; ++i)
  178. {
  179. jassert (destSamples[i] != nullptr);
  180. const int srcChan = jmin (i, (int) numChannels - 1);
  181. const int16* src = rawData + srcChan;
  182. int* const dst = destSamples[i] + startOffsetInDestBuffer;
  183. for (int j = 0; j < numToDo; ++j)
  184. {
  185. dst[j] = ((uint32) *src) << 16;
  186. src += numChannels;
  187. }
  188. }
  189. bufferStart += numToDo * stride;
  190. startOffsetInDestBuffer += numToDo;
  191. numSamples -= numToDo;
  192. currentPosition += numToDo;
  193. }
  194. return true;
  195. }
  196. bool ok;
  197. private:
  198. DynamicLibrary wmvCoreLib;
  199. ComSmartPtr<IWMSyncReader> wmSyncReader;
  200. int64 currentPosition;
  201. MemoryBlock buffer;
  202. int bufferStart, bufferEnd;
  203. void scanFileForDetails()
  204. {
  205. ComSmartPtr<IWMHeaderInfo> wmHeaderInfo;
  206. HRESULT hr = wmSyncReader.QueryInterface (wmHeaderInfo);
  207. QWORD lengthInNanoseconds = 0;
  208. WORD lengthOfLength = sizeof (lengthInNanoseconds);
  209. WORD wmStreamNum = 0;
  210. WMT_ATTR_DATATYPE wmAttrDataType;
  211. hr = wmHeaderInfo->GetAttributeByName (&wmStreamNum, L"Duration", &wmAttrDataType,
  212. (BYTE*) &lengthInNanoseconds, &lengthOfLength);
  213. ComSmartPtr<IWMStreamConfig> wmStreamConfig;
  214. ComSmartPtr<IWMProfile> wmProfile;
  215. hr = wmSyncReader.QueryInterface (wmProfile);
  216. hr = wmProfile->GetStream (0, wmStreamConfig.resetAndGetPointerAddress());
  217. ComSmartPtr<IWMMediaProps> wmMediaProperties;
  218. hr = wmStreamConfig.QueryInterface (wmMediaProperties);
  219. DWORD sizeMediaType;
  220. hr = wmMediaProperties->GetMediaType (0, &sizeMediaType);
  221. HeapBlock<WM_MEDIA_TYPE> mediaType;
  222. mediaType.malloc (sizeMediaType, 1);
  223. hr = wmMediaProperties->GetMediaType (mediaType, &sizeMediaType);
  224. if (mediaType->majortype == WMMEDIATYPE_Audio)
  225. {
  226. const WAVEFORMATEX* const inputFormat = reinterpret_cast<WAVEFORMATEX*> (mediaType->pbFormat);
  227. sampleRate = inputFormat->nSamplesPerSec;
  228. numChannels = inputFormat->nChannels;
  229. bitsPerSample = inputFormat->wBitsPerSample;
  230. lengthInSamples = (lengthInNanoseconds * sampleRate) / 10000000;
  231. }
  232. }
  233. JUCE_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR (WMAudioReader);
  234. };
  235. }
  236. //==============================================================================
  237. WindowsMediaAudioFormat::WindowsMediaAudioFormat()
  238. : AudioFormat (TRANS (WindowsMediaCodec::wmFormatName), StringArray (WindowsMediaCodec::extensions))
  239. {
  240. }
  241. WindowsMediaAudioFormat::~WindowsMediaAudioFormat() {}
  242. Array<int> WindowsMediaAudioFormat::getPossibleSampleRates() { return Array<int>(); }
  243. Array<int> WindowsMediaAudioFormat::getPossibleBitDepths() { return Array<int>(); }
  244. bool WindowsMediaAudioFormat::canDoStereo() { return true; }
  245. bool WindowsMediaAudioFormat::canDoMono() { return true; }
  246. //==============================================================================
  247. AudioFormatReader* WindowsMediaAudioFormat::createReaderFor (InputStream* sourceStream, bool deleteStreamIfOpeningFails)
  248. {
  249. ScopedPointer<WindowsMediaCodec::WMAudioReader> r (new WindowsMediaCodec::WMAudioReader (sourceStream));
  250. if (r->ok)
  251. return r.release();
  252. if (! deleteStreamIfOpeningFails)
  253. r->input = nullptr;
  254. return nullptr;
  255. }
  256. AudioFormatWriter* WindowsMediaAudioFormat::createWriterFor (OutputStream* /*streamToWriteTo*/, double /*sampleRateToUse*/,
  257. unsigned int /*numberOfChannels*/, int /*bitsPerSample*/,
  258. const StringPairArray& /*metadataValues*/, int /*qualityOptionIndex*/)
  259. {
  260. jassertfalse; // not yet implemented!
  261. return nullptr;
  262. }
  263. #endif