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.

350 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. wmvCoreLib ("Wmvcore.dll"),
  103. currentPosition (0),
  104. bufferStart (0), bufferEnd (0)
  105. {
  106. typedef HRESULT (*WMCreateSyncReaderType) (IUnknown*, DWORD, IWMSyncReader**);
  107. WMCreateSyncReaderType wmCreateSyncReader = nullptr;
  108. wmCreateSyncReader = (WMCreateSyncReaderType) wmvCoreLib.getFunction ("WMCreateSyncReader");
  109. if (wmCreateSyncReader != nullptr)
  110. {
  111. CoInitialize (0);
  112. HRESULT hr = wmCreateSyncReader (nullptr, WMT_RIGHT_PLAYBACK, wmSyncReader.resetAndGetPointerAddress());
  113. if (SUCCEEDED (hr))
  114. hr = wmSyncReader->OpenStream (new JuceIStream (*input));
  115. if (SUCCEEDED (hr))
  116. {
  117. WORD streamNum = 1;
  118. hr = wmSyncReader->GetStreamNumberForOutput (0, &streamNum);
  119. hr = wmSyncReader->SetReadStreamSamples (streamNum, false);
  120. scanFileForDetails();
  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 (sampleRate <= 0)
  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. private:
  197. DynamicLibrary wmvCoreLib;
  198. ComSmartPtr<IWMSyncReader> wmSyncReader;
  199. int64 currentPosition;
  200. MemoryBlock buffer;
  201. int bufferStart, bufferEnd;
  202. void scanFileForDetails()
  203. {
  204. ComSmartPtr<IWMHeaderInfo> wmHeaderInfo;
  205. HRESULT hr = wmSyncReader.QueryInterface (wmHeaderInfo);
  206. if (SUCCEEDED (hr))
  207. {
  208. QWORD lengthInNanoseconds = 0;
  209. WORD lengthOfLength = sizeof (lengthInNanoseconds);
  210. WORD streamNum = 0;
  211. WMT_ATTR_DATATYPE wmAttrDataType;
  212. hr = wmHeaderInfo->GetAttributeByName (&streamNum, L"Duration", &wmAttrDataType,
  213. (BYTE*) &lengthInNanoseconds, &lengthOfLength);
  214. ComSmartPtr<IWMProfile> wmProfile;
  215. hr = wmSyncReader.QueryInterface (wmProfile);
  216. if (SUCCEEDED (hr))
  217. {
  218. ComSmartPtr<IWMStreamConfig> wmStreamConfig;
  219. hr = wmProfile->GetStream (0, wmStreamConfig.resetAndGetPointerAddress());
  220. if (SUCCEEDED (hr))
  221. {
  222. ComSmartPtr<IWMMediaProps> wmMediaProperties;
  223. hr = wmStreamConfig.QueryInterface (wmMediaProperties);
  224. if (SUCCEEDED (hr))
  225. {
  226. DWORD sizeMediaType;
  227. hr = wmMediaProperties->GetMediaType (0, &sizeMediaType);
  228. HeapBlock<WM_MEDIA_TYPE> mediaType;
  229. mediaType.malloc (sizeMediaType, 1);
  230. hr = wmMediaProperties->GetMediaType (mediaType, &sizeMediaType);
  231. if (mediaType->majortype == WMMEDIATYPE_Audio)
  232. {
  233. const WAVEFORMATEX* const inputFormat = reinterpret_cast<WAVEFORMATEX*> (mediaType->pbFormat);
  234. sampleRate = inputFormat->nSamplesPerSec;
  235. numChannels = inputFormat->nChannels;
  236. bitsPerSample = inputFormat->wBitsPerSample;
  237. lengthInSamples = (lengthInNanoseconds * sampleRate) / 10000000;
  238. }
  239. }
  240. }
  241. }
  242. }
  243. }
  244. JUCE_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR (WMAudioReader);
  245. };
  246. }
  247. //==============================================================================
  248. WindowsMediaAudioFormat::WindowsMediaAudioFormat()
  249. : AudioFormat (TRANS (WindowsMediaCodec::wmFormatName), StringArray (WindowsMediaCodec::extensions))
  250. {
  251. }
  252. WindowsMediaAudioFormat::~WindowsMediaAudioFormat() {}
  253. Array<int> WindowsMediaAudioFormat::getPossibleSampleRates() { return Array<int>(); }
  254. Array<int> WindowsMediaAudioFormat::getPossibleBitDepths() { return Array<int>(); }
  255. bool WindowsMediaAudioFormat::canDoStereo() { return true; }
  256. bool WindowsMediaAudioFormat::canDoMono() { return true; }
  257. //==============================================================================
  258. AudioFormatReader* WindowsMediaAudioFormat::createReaderFor (InputStream* sourceStream, bool deleteStreamIfOpeningFails)
  259. {
  260. ScopedPointer<WindowsMediaCodec::WMAudioReader> r (new WindowsMediaCodec::WMAudioReader (sourceStream));
  261. if (r->sampleRate > 0)
  262. return r.release();
  263. if (! deleteStreamIfOpeningFails)
  264. r->input = nullptr;
  265. return nullptr;
  266. }
  267. AudioFormatWriter* WindowsMediaAudioFormat::createWriterFor (OutputStream* /*streamToWriteTo*/, double /*sampleRateToUse*/,
  268. unsigned int /*numberOfChannels*/, int /*bitsPerSample*/,
  269. const StringPairArray& /*metadataValues*/, int /*qualityOptionIndex*/)
  270. {
  271. jassertfalse; // not yet implemented!
  272. return nullptr;
  273. }
  274. #endif