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.

723 lines
30KB

  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. //==============================================================================
  19. static const char* const aiffFormatName = "AIFF file";
  20. static const char* const aiffExtensions[] = { ".aiff", ".aif", 0 };
  21. //==============================================================================
  22. namespace AiffFileHelpers
  23. {
  24. inline int chunkName (const char* const name) { return (int) ByteOrder::littleEndianInt (name); }
  25. #if JUCE_MSVC
  26. #pragma pack (push, 1)
  27. #endif
  28. //==============================================================================
  29. struct InstChunk
  30. {
  31. struct Loop
  32. {
  33. uint16 type; // these are different in AIFF and WAV
  34. uint16 startIdentifier;
  35. uint16 endIdentifier;
  36. } JUCE_PACKED;
  37. int8 baseNote;
  38. int8 detune;
  39. int8 lowNote;
  40. int8 highNote;
  41. int8 lowVelocity;
  42. int8 highVelocity;
  43. int16 gain;
  44. Loop sustainLoop;
  45. Loop releaseLoop;
  46. void copyTo (StringPairArray& values) const
  47. {
  48. values.set ("MidiUnityNote", String (baseNote));
  49. values.set ("Detune", String (detune));
  50. values.set ("LowNote", String (lowNote));
  51. values.set ("HighNote", String (highNote));
  52. values.set ("LowVelocity", String (lowVelocity));
  53. values.set ("HighVelocity", String (highVelocity));
  54. values.set ("Gain", String ((int16) ByteOrder::swapIfLittleEndian ((uint16) gain)));
  55. values.set ("NumSampleLoops", String (2)); // always 2 with AIFF, WAV can have more
  56. values.set ("Loop0Type", String (ByteOrder::swapIfLittleEndian (sustainLoop.type)));
  57. values.set ("Loop0StartIdentifier", String (ByteOrder::swapIfLittleEndian (sustainLoop.startIdentifier)));
  58. values.set ("Loop0EndIdentifier", String (ByteOrder::swapIfLittleEndian (sustainLoop.endIdentifier)));
  59. values.set ("Loop1Type", String (ByteOrder::swapIfLittleEndian (releaseLoop.type)));
  60. values.set ("Loop1StartIdentifier", String (ByteOrder::swapIfLittleEndian (releaseLoop.startIdentifier)));
  61. values.set ("Loop1EndIdentifier", String (ByteOrder::swapIfLittleEndian (releaseLoop.endIdentifier)));
  62. }
  63. static uint16 getValue16 (const StringPairArray& values, const char* name, const char* def)
  64. {
  65. return ByteOrder::swapIfLittleEndian ((uint16) values.getValue (name, def).getIntValue());
  66. }
  67. static int8 getValue8 (const StringPairArray& values, const char* name, const char* def)
  68. {
  69. return (int8) values.getValue (name, def).getIntValue();
  70. }
  71. static void create (MemoryBlock& block, const StringPairArray& values)
  72. {
  73. if (values.getAllKeys().contains ("MidiUnityNote", true))
  74. {
  75. block.setSize ((sizeof (InstChunk) + 3) & ~(size_t) 3, true);
  76. InstChunk& inst = *static_cast <InstChunk*> (block.getData());
  77. inst.baseNote = getValue8 (values, "MidiUnityNote", "60");
  78. inst.detune = getValue8 (values, "Detune", "0");
  79. inst.lowNote = getValue8 (values, "LowNote", "0");
  80. inst.highNote = getValue8 (values, "HighNote", "127");
  81. inst.lowVelocity = getValue8 (values, "LowVelocity", "1");
  82. inst.highVelocity = getValue8 (values, "HighVelocity", "127");
  83. inst.gain = (int16) getValue16 (values, "Gain", "0");
  84. inst.sustainLoop.type = getValue16 (values, "Loop0Type", "0");
  85. inst.sustainLoop.startIdentifier = getValue16 (values, "Loop0StartIdentifier", "0");
  86. inst.sustainLoop.endIdentifier = getValue16 (values, "Loop0EndIdentifier", "0");
  87. inst.releaseLoop.type = getValue16 (values, "Loop1Type", "0");
  88. inst.releaseLoop.startIdentifier = getValue16 (values, "Loop1StartIdentifier", "0");
  89. inst.releaseLoop.endIdentifier = getValue16 (values, "Loop1EndIdentifier", "0");
  90. }
  91. }
  92. } JUCE_PACKED;
  93. #if JUCE_MSVC
  94. #pragma pack (pop)
  95. #endif
  96. //==============================================================================
  97. namespace MarkChunk
  98. {
  99. static bool metaDataContainsZeroIdentifiers (const StringPairArray& values)
  100. {
  101. // (zero cue identifiers are valid for WAV but not for AIFF)
  102. const String cueString ("Cue");
  103. const String noteString ("CueNote");
  104. const String identifierString ("Identifier");
  105. const StringArray& keys = values.getAllKeys();
  106. for (int i = 0; i < keys.size(); ++i)
  107. {
  108. const String key (keys[i]);
  109. if (key.startsWith (noteString))
  110. continue; // zero identifier IS valid in a COMT chunk
  111. if (key.startsWith (cueString) && key.contains (identifierString))
  112. {
  113. const int value = values.getValue (key, "-1").getIntValue();
  114. if (value == 0)
  115. return true;
  116. }
  117. }
  118. return false;
  119. }
  120. static void create (MemoryBlock& block, const StringPairArray& values)
  121. {
  122. const int numCues = values.getValue ("NumCuePoints", "0").getIntValue();
  123. if (numCues > 0)
  124. {
  125. MemoryOutputStream out (block, false);
  126. out.writeShortBigEndian ((short) numCues);
  127. const int numCueLabels = values.getValue ("NumCueLabels", "0").getIntValue();
  128. const int idOffset = metaDataContainsZeroIdentifiers (values) ? 1 : 0; // can't have zero IDs in AIFF
  129. #if JUCE_DEBUG
  130. Array<int> identifiers;
  131. #endif
  132. for (int i = 0; i < numCues; ++i)
  133. {
  134. const String prefixCue ("Cue" + String (i));
  135. const int identifier = idOffset + values.getValue (prefixCue + "Identifier", "1").getIntValue();
  136. #if JUCE_DEBUG
  137. jassert (! identifiers.contains (identifier));
  138. identifiers.add (identifier);
  139. #endif
  140. const int offset = values.getValue (prefixCue + "Offset", "0").getIntValue();
  141. String label ("CueLabel" + String (i));
  142. for (int labelIndex = 0; labelIndex < numCueLabels; ++labelIndex)
  143. {
  144. const String prefixLabel ("CueLabel" + String (labelIndex));
  145. const int labelIdentifier = idOffset + values.getValue (prefixLabel + "Identifier", "1").getIntValue();
  146. if (labelIdentifier == identifier)
  147. {
  148. label = values.getValue (prefixLabel + "Text", label);
  149. break;
  150. }
  151. }
  152. out.writeShortBigEndian ((short) identifier);
  153. out.writeIntBigEndian (offset);
  154. const int labelLength = jmin (254, label.getNumBytesAsUTF8()); // seems to need null terminator even though it's a pstring
  155. out.writeByte ((char) labelLength + 1);
  156. out.write (label.toUTF8(), labelLength);
  157. out.writeByte (0);
  158. }
  159. if ((out.getDataSize() & 1) != 0)
  160. out.writeByte (0);
  161. }
  162. }
  163. }
  164. //==============================================================================
  165. namespace COMTChunk
  166. {
  167. static void create (MemoryBlock& block, const StringPairArray& values)
  168. {
  169. const int numNotes = values.getValue ("NumCueNotes", "0").getIntValue();
  170. if (numNotes > 0)
  171. {
  172. MemoryOutputStream out (block, false);
  173. out.writeShortBigEndian ((short) numNotes);
  174. for (int i = 0; i < numNotes; ++i)
  175. {
  176. const String prefix ("CueNote" + String (i));
  177. out.writeIntBigEndian (values.getValue (prefix + "TimeStamp", "0").getIntValue());
  178. out.writeShortBigEndian ((short) values.getValue (prefix + "Identifier", "0").getIntValue());
  179. const String comment (values.getValue (prefix + "Text", String::empty));
  180. out.write (comment.toUTF8(), jmin (comment.getNumBytesAsUTF8(), 65534));
  181. out.writeByte (0);
  182. if ((out.getDataSize() & 1) != 0)
  183. out.writeByte (0);
  184. }
  185. }
  186. }
  187. }
  188. }
  189. //==============================================================================
  190. class AiffAudioFormatReader : public AudioFormatReader
  191. {
  192. public:
  193. AiffAudioFormatReader (InputStream* in)
  194. : AudioFormatReader (in, TRANS (aiffFormatName))
  195. {
  196. using namespace AiffFileHelpers;
  197. if (input->readInt() == chunkName ("FORM"))
  198. {
  199. const int len = input->readIntBigEndian();
  200. const int64 end = input->getPosition() + len;
  201. const int nextType = input->readInt();
  202. if (nextType == chunkName ("AIFF") || nextType == chunkName ("AIFC"))
  203. {
  204. bool hasGotVer = false;
  205. bool hasGotData = false;
  206. bool hasGotType = false;
  207. while (input->getPosition() < end)
  208. {
  209. const int type = input->readInt();
  210. const uint32 length = (uint32) input->readIntBigEndian();
  211. const int64 chunkEnd = input->getPosition() + length;
  212. if (type == chunkName ("FVER"))
  213. {
  214. hasGotVer = true;
  215. const int ver = input->readIntBigEndian();
  216. if (ver != 0 && ver != (int) 0xa2805140)
  217. break;
  218. }
  219. else if (type == chunkName ("COMM"))
  220. {
  221. hasGotType = true;
  222. numChannels = (unsigned int) input->readShortBigEndian();
  223. lengthInSamples = input->readIntBigEndian();
  224. bitsPerSample = (unsigned int) input->readShortBigEndian();
  225. bytesPerFrame = (int) ((numChannels * bitsPerSample) >> 3);
  226. unsigned char sampleRateBytes[10];
  227. input->read (sampleRateBytes, 10);
  228. const int byte0 = sampleRateBytes[0];
  229. if ((byte0 & 0x80) != 0
  230. || byte0 <= 0x3F || byte0 > 0x40
  231. || (byte0 == 0x40 && sampleRateBytes[1] > 0x1C))
  232. break;
  233. unsigned int sampRate = ByteOrder::bigEndianInt (sampleRateBytes + 2);
  234. sampRate >>= (16414 - ByteOrder::bigEndianShort (sampleRateBytes));
  235. sampleRate = (int) sampRate;
  236. if (length <= 18)
  237. {
  238. // some types don't have a chunk large enough to include a compression
  239. // type, so assume it's just big-endian pcm
  240. littleEndian = false;
  241. }
  242. else
  243. {
  244. const int compType = input->readInt();
  245. if (compType == chunkName ("NONE") || compType == chunkName ("twos"))
  246. {
  247. littleEndian = false;
  248. }
  249. else if (compType == chunkName ("sowt"))
  250. {
  251. littleEndian = true;
  252. }
  253. else
  254. {
  255. sampleRate = 0;
  256. break;
  257. }
  258. }
  259. }
  260. else if (type == chunkName ("SSND"))
  261. {
  262. hasGotData = true;
  263. const int offset = input->readIntBigEndian();
  264. dataChunkStart = input->getPosition() + 4 + offset;
  265. lengthInSamples = (bytesPerFrame > 0) ? jmin (lengthInSamples, ((int64) length) / (int64) bytesPerFrame) : 0;
  266. }
  267. else if (type == chunkName ("MARK"))
  268. {
  269. const uint16 numCues = (uint16) input->readShortBigEndian();
  270. // these two are always the same for AIFF-read files
  271. metadataValues.set ("NumCuePoints", String (numCues));
  272. metadataValues.set ("NumCueLabels", String (numCues));
  273. for (uint16 i = 0; i < numCues; ++i)
  274. {
  275. uint16 identifier = (uint16) input->readShortBigEndian();
  276. uint32 offset = (uint32) input->readIntBigEndian();
  277. uint8 stringLength = (uint8) input->readByte();
  278. MemoryBlock textBlock;
  279. input->readIntoMemoryBlock (textBlock, stringLength);
  280. // if the stringLength is even then read one more byte as the
  281. // string needs to be an even number of bytes INCLUDING the
  282. // leading length character in the pascal string
  283. if ((stringLength & 1) == 0)
  284. input->readByte();
  285. const String prefixCue ("Cue" + String (i));
  286. metadataValues.set (prefixCue + "Identifier", String (identifier));
  287. metadataValues.set (prefixCue + "Offset", String (offset));
  288. const String prefixLabel ("CueLabel" + String (i));
  289. metadataValues.set (prefixLabel + "Identifier", String (identifier));
  290. metadataValues.set (prefixLabel + "Text", textBlock.toString());
  291. }
  292. }
  293. else if (type == chunkName ("COMT"))
  294. {
  295. const uint16 numNotes = (uint16) input->readShortBigEndian();
  296. metadataValues.set ("NumCueNotes", String (numNotes));
  297. for (uint16 i = 0; i < numNotes; ++i)
  298. {
  299. uint32 timestamp = (uint32) input->readIntBigEndian();
  300. uint16 identifier = (uint16) input->readShortBigEndian(); // may be zero in this case
  301. uint16 stringLength = (uint16) input->readShortBigEndian();
  302. MemoryBlock textBlock;
  303. input->readIntoMemoryBlock (textBlock, stringLength + (stringLength & 1));
  304. const String prefix ("CueNote" + String (i));
  305. metadataValues.set (prefix + "TimeStamp", String (timestamp));
  306. metadataValues.set (prefix + "Identifier", String (identifier));
  307. metadataValues.set (prefix + "Text", textBlock.toString());
  308. }
  309. }
  310. else if (type == chunkName ("INST"))
  311. {
  312. HeapBlock <InstChunk> inst;
  313. inst.calloc (jmax ((size_t) length + 1, sizeof (InstChunk)), 1);
  314. input->read (inst, (int) length);
  315. inst->copyTo (metadataValues);
  316. }
  317. else if ((hasGotVer && hasGotData && hasGotType)
  318. || chunkEnd < input->getPosition()
  319. || input->isExhausted())
  320. {
  321. break;
  322. }
  323. input->setPosition (chunkEnd + (chunkEnd & 1)); // (chunks should be aligned to an even byte address)
  324. }
  325. }
  326. }
  327. if (metadataValues.size() > 0)
  328. metadataValues.set ("MetaDataSource", "AIFF");
  329. }
  330. //==============================================================================
  331. bool readSamples (int** destSamples, int numDestChannels, int startOffsetInDestBuffer,
  332. int64 startSampleInFile, int numSamples)
  333. {
  334. const int64 samplesAvailable = lengthInSamples - startSampleInFile;
  335. if (samplesAvailable < numSamples)
  336. {
  337. for (int i = numDestChannels; --i >= 0;)
  338. if (destSamples[i] != nullptr)
  339. zeromem (destSamples[i] + startOffsetInDestBuffer, sizeof (int) * (size_t) numSamples);
  340. numSamples = (int) samplesAvailable;
  341. }
  342. if (numSamples <= 0)
  343. return true;
  344. input->setPosition (dataChunkStart + startSampleInFile * bytesPerFrame);
  345. while (numSamples > 0)
  346. {
  347. const int tempBufSize = 480 * 3 * 4; // (keep this a multiple of 3)
  348. char tempBuffer [tempBufSize];
  349. const int numThisTime = jmin (tempBufSize / bytesPerFrame, numSamples);
  350. const int bytesRead = input->read (tempBuffer, numThisTime * bytesPerFrame);
  351. if (bytesRead < numThisTime * bytesPerFrame)
  352. {
  353. jassert (bytesRead >= 0);
  354. zeromem (tempBuffer + bytesRead, (size_t) (numThisTime * bytesPerFrame - bytesRead));
  355. }
  356. jassert (! usesFloatingPointData); // (would need to add support for this if it's possible)
  357. if (littleEndian)
  358. {
  359. switch (bitsPerSample)
  360. {
  361. case 8: ReadHelper<AudioData::Int32, AudioData::Int8, AudioData::LittleEndian>::read (destSamples, startOffsetInDestBuffer, numDestChannels, tempBuffer, (int) numChannels, numThisTime); break;
  362. case 16: ReadHelper<AudioData::Int32, AudioData::Int16, AudioData::LittleEndian>::read (destSamples, startOffsetInDestBuffer, numDestChannels, tempBuffer, (int) numChannels, numThisTime); break;
  363. case 24: ReadHelper<AudioData::Int32, AudioData::Int24, AudioData::LittleEndian>::read (destSamples, startOffsetInDestBuffer, numDestChannels, tempBuffer, (int) numChannels, numThisTime); break;
  364. case 32: ReadHelper<AudioData::Int32, AudioData::Int32, AudioData::LittleEndian>::read (destSamples, startOffsetInDestBuffer, numDestChannels, tempBuffer, (int) numChannels, numThisTime); break;
  365. default: jassertfalse; break;
  366. }
  367. }
  368. else
  369. {
  370. switch (bitsPerSample)
  371. {
  372. case 8: ReadHelper<AudioData::Int32, AudioData::Int8, AudioData::BigEndian>::read (destSamples, startOffsetInDestBuffer, numDestChannels, tempBuffer, (int) numChannels, numThisTime); break;
  373. case 16: ReadHelper<AudioData::Int32, AudioData::Int16, AudioData::BigEndian>::read (destSamples, startOffsetInDestBuffer, numDestChannels, tempBuffer, (int) numChannels, numThisTime); break;
  374. case 24: ReadHelper<AudioData::Int32, AudioData::Int24, AudioData::BigEndian>::read (destSamples, startOffsetInDestBuffer, numDestChannels, tempBuffer, (int) numChannels, numThisTime); break;
  375. case 32: ReadHelper<AudioData::Int32, AudioData::Int32, AudioData::BigEndian>::read (destSamples, startOffsetInDestBuffer, numDestChannels, tempBuffer, (int) numChannels, numThisTime); break;
  376. default: jassertfalse; break;
  377. }
  378. }
  379. startOffsetInDestBuffer += numThisTime;
  380. numSamples -= numThisTime;
  381. }
  382. return true;
  383. }
  384. int bytesPerFrame;
  385. int64 dataChunkStart;
  386. bool littleEndian;
  387. private:
  388. JUCE_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR (AiffAudioFormatReader)
  389. };
  390. //==============================================================================
  391. class AiffAudioFormatWriter : public AudioFormatWriter
  392. {
  393. public:
  394. AiffAudioFormatWriter (OutputStream* out, double sampleRate_,
  395. unsigned int numChans, unsigned int bits,
  396. const StringPairArray& metadataValues)
  397. : AudioFormatWriter (out, TRANS (aiffFormatName), sampleRate_, numChans, bits),
  398. lengthInSamples (0),
  399. bytesWritten (0),
  400. writeFailed (false)
  401. {
  402. using namespace AiffFileHelpers;
  403. if (metadataValues.size() > 0)
  404. {
  405. // The meta data should have been santised for the AIFF format.
  406. // If it was originally sourced from a WAV file the MetaDataSource
  407. // key should be removed (or set to "AIFF") once this has been done
  408. jassert (metadataValues.getValue ("MetaDataSource", "None") != "WAV");
  409. MarkChunk::create (markChunk, metadataValues);
  410. COMTChunk::create (comtChunk, metadataValues);
  411. InstChunk::create (instChunk, metadataValues);
  412. }
  413. headerPosition = out->getPosition();
  414. writeHeader();
  415. }
  416. ~AiffAudioFormatWriter()
  417. {
  418. if ((bytesWritten & 1) != 0)
  419. output->writeByte (0);
  420. writeHeader();
  421. }
  422. //==============================================================================
  423. bool write (const int** data, int numSamples)
  424. {
  425. jassert (data != nullptr && *data != nullptr); // the input must contain at least one channel!
  426. if (writeFailed)
  427. return false;
  428. const size_t bytes = (size_t) numSamples * numChannels * bitsPerSample / 8;
  429. tempBlock.ensureSize ((size_t) bytes, false);
  430. switch (bitsPerSample)
  431. {
  432. case 8: WriteHelper<AudioData::Int8, AudioData::Int32, AudioData::BigEndian>::write (tempBlock.getData(), (int) numChannels, data, numSamples); break;
  433. case 16: WriteHelper<AudioData::Int16, AudioData::Int32, AudioData::BigEndian>::write (tempBlock.getData(), (int) numChannels, data, numSamples); break;
  434. case 24: WriteHelper<AudioData::Int24, AudioData::Int32, AudioData::BigEndian>::write (tempBlock.getData(), (int) numChannels, data, numSamples); break;
  435. case 32: WriteHelper<AudioData::Int32, AudioData::Int32, AudioData::BigEndian>::write (tempBlock.getData(), (int) numChannels, data, numSamples); break;
  436. default: jassertfalse; break;
  437. }
  438. if (bytesWritten + bytes >= (size_t) 0xfff00000
  439. || ! output->write (tempBlock.getData(), (int) bytes))
  440. {
  441. // failed to write to disk, so let's try writing the header.
  442. // If it's just run out of disk space, then if it does manage
  443. // to write the header, we'll still have a useable file..
  444. writeHeader();
  445. writeFailed = true;
  446. return false;
  447. }
  448. else
  449. {
  450. bytesWritten += bytes;
  451. lengthInSamples += (uint64) numSamples;
  452. return true;
  453. }
  454. }
  455. private:
  456. MemoryBlock tempBlock, markChunk, comtChunk, instChunk;
  457. uint64 lengthInSamples, bytesWritten;
  458. int64 headerPosition;
  459. bool writeFailed;
  460. void writeHeader()
  461. {
  462. using namespace AiffFileHelpers;
  463. const bool couldSeekOk = output->setPosition (headerPosition);
  464. (void) couldSeekOk;
  465. // if this fails, you've given it an output stream that can't seek! It needs
  466. // to be able to seek back to write the header
  467. jassert (couldSeekOk);
  468. const int headerLen = (int) (54 + (markChunk.getSize() > 0 ? markChunk.getSize() + 8 : 0)
  469. + (comtChunk.getSize() > 0 ? comtChunk.getSize() + 8 : 0)
  470. + (instChunk.getSize() > 0 ? instChunk.getSize() + 8 : 0));
  471. int audioBytes = (int) (lengthInSamples * ((bitsPerSample * numChannels) / 8));
  472. audioBytes += (audioBytes & 1);
  473. output->writeInt (chunkName ("FORM"));
  474. output->writeIntBigEndian (headerLen + audioBytes - 8);
  475. output->writeInt (chunkName ("AIFF"));
  476. output->writeInt (chunkName ("COMM"));
  477. output->writeIntBigEndian (18);
  478. output->writeShortBigEndian ((short) numChannels);
  479. output->writeIntBigEndian ((int) lengthInSamples);
  480. output->writeShortBigEndian ((short) bitsPerSample);
  481. uint8 sampleRateBytes[10] = { 0 };
  482. if (sampleRate <= 1)
  483. {
  484. sampleRateBytes[0] = 0x3f;
  485. sampleRateBytes[1] = 0xff;
  486. sampleRateBytes[2] = 0x80;
  487. }
  488. else
  489. {
  490. int mask = 0x40000000;
  491. sampleRateBytes[0] = 0x40;
  492. if (sampleRate >= mask)
  493. {
  494. jassertfalse;
  495. sampleRateBytes[1] = 0x1d;
  496. }
  497. else
  498. {
  499. int n = (int) sampleRate;
  500. int i;
  501. for (i = 0; i <= 32 ; ++i)
  502. {
  503. if ((n & mask) != 0)
  504. break;
  505. mask >>= 1;
  506. }
  507. n = n << (i + 1);
  508. sampleRateBytes[1] = (uint8) (29 - i);
  509. sampleRateBytes[2] = (uint8) ((n >> 24) & 0xff);
  510. sampleRateBytes[3] = (uint8) ((n >> 16) & 0xff);
  511. sampleRateBytes[4] = (uint8) ((n >> 8) & 0xff);
  512. sampleRateBytes[5] = (uint8) (n & 0xff);
  513. }
  514. }
  515. output->write (sampleRateBytes, 10);
  516. if (markChunk.getSize() > 0)
  517. {
  518. output->writeInt (chunkName ("MARK"));
  519. output->writeIntBigEndian ((int) markChunk.getSize());
  520. *output << markChunk;
  521. }
  522. if (comtChunk.getSize() > 0)
  523. {
  524. output->writeInt (chunkName ("COMT"));
  525. output->writeIntBigEndian ((int) comtChunk.getSize());
  526. *output << comtChunk;
  527. }
  528. if (instChunk.getSize() > 0)
  529. {
  530. output->writeInt (chunkName ("INST"));
  531. output->writeIntBigEndian ((int) instChunk.getSize());
  532. *output << instChunk;
  533. }
  534. output->writeInt (chunkName ("SSND"));
  535. output->writeIntBigEndian (audioBytes + 8);
  536. output->writeInt (0);
  537. output->writeInt (0);
  538. jassert (output->getPosition() == headerLen);
  539. }
  540. JUCE_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR (AiffAudioFormatWriter)
  541. };
  542. //==============================================================================
  543. AiffAudioFormat::AiffAudioFormat()
  544. : AudioFormat (TRANS (aiffFormatName), StringArray (aiffExtensions))
  545. {
  546. }
  547. AiffAudioFormat::~AiffAudioFormat()
  548. {
  549. }
  550. Array<int> AiffAudioFormat::getPossibleSampleRates()
  551. {
  552. const int rates[] = { 22050, 32000, 44100, 48000, 88200, 96000, 176400, 192000, 0 };
  553. return Array <int> (rates);
  554. }
  555. Array<int> AiffAudioFormat::getPossibleBitDepths()
  556. {
  557. const int depths[] = { 8, 16, 24, 0 };
  558. return Array <int> (depths);
  559. }
  560. bool AiffAudioFormat::canDoStereo() { return true; }
  561. bool AiffAudioFormat::canDoMono() { return true; }
  562. #if JUCE_MAC
  563. bool AiffAudioFormat::canHandleFile (const File& f)
  564. {
  565. if (AudioFormat::canHandleFile (f))
  566. return true;
  567. const OSType type = f.getMacOSType();
  568. return type == 'AIFF' || type == 'AIFC'
  569. || type == 'aiff' || type == 'aifc';
  570. }
  571. #endif
  572. AudioFormatReader* AiffAudioFormat::createReaderFor (InputStream* sourceStream, const bool deleteStreamIfOpeningFails)
  573. {
  574. ScopedPointer <AiffAudioFormatReader> w (new AiffAudioFormatReader (sourceStream));
  575. if (w->sampleRate > 0 && w->numChannels > 0)
  576. return w.release();
  577. if (! deleteStreamIfOpeningFails)
  578. w->input = nullptr;
  579. return nullptr;
  580. }
  581. AudioFormatWriter* AiffAudioFormat::createWriterFor (OutputStream* out,
  582. double sampleRate,
  583. unsigned int numberOfChannels,
  584. int bitsPerSample,
  585. const StringPairArray& metadataValues,
  586. int /*qualityOptionIndex*/)
  587. {
  588. if (getPossibleBitDepths().contains (bitsPerSample))
  589. return new AiffAudioFormatWriter (out, sampleRate, numberOfChannels, (unsigned int) bitsPerSample, metadataValues);
  590. return nullptr;
  591. }