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.

162 lines
6.0KB

  1. /*
  2. ==============================================================================
  3. This file is part of the dRowAudio JUCE module
  4. Copyright 2004-13 by dRowAudio.
  5. ------------------------------------------------------------------------------
  6. dRowAudio is provided under the terms of The MIT License (MIT):
  7. Permission is hereby granted, free of charge, to any person obtaining a copy
  8. of this software and associated documentation files (the "Software"), to deal
  9. in the Software without restriction, including without limitation the rights
  10. to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  11. copies of the Software, and to permit persons to whom the Software is
  12. furnished to do so, subject to the following conditions:
  13. The above copyright notice and this permission notice shall be included in all
  14. copies or substantial portions of the Software.
  15. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  16. IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  17. FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  18. AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  19. LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  20. OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
  21. SOFTWARE.
  22. ==============================================================================
  23. */
  24. //==============================================================================
  25. namespace
  26. {
  27. const char* const audioSampleBufferAudioFormatName = "AudioSampleBuffer format stream";
  28. }
  29. //==============================================================================
  30. class AudioSampleBufferReader : public AudioFormatReader
  31. {
  32. public:
  33. AudioSampleBufferReader (InputStream* const inp)
  34. : AudioFormatReader (inp, TRANS (audioSampleBufferAudioFormatName)),
  35. ok (false)
  36. {
  37. usesFloatingPointData = true;
  38. if (inp != nullptr)
  39. {
  40. ok = isAudioSampleBuffer (*inp, numChannels, lengthInSamples);
  41. sampleRate = 44100;
  42. bitsPerSample = 32;
  43. }
  44. }
  45. ~AudioSampleBufferReader()
  46. {
  47. }
  48. //==============================================================================
  49. bool readSamples (int** destSamples, int numDestChannels, int startOffsetInDestBuffer,
  50. int64 startSampleInFile, int numSamples)
  51. {
  52. jassert (destSamples != nullptr);
  53. const int64 samplesAvailable = lengthInSamples - startSampleInFile;
  54. if (samplesAvailable < numSamples)
  55. {
  56. for (int i = numDestChannels; --i >= 0;)
  57. if (destSamples[i] != nullptr)
  58. zeromem (destSamples[i] + startOffsetInDestBuffer, sizeof (int) * numSamples);
  59. numSamples = (int) samplesAvailable;
  60. }
  61. if (numSamples <= 0)
  62. return true;
  63. while (numSamples > 0)
  64. {
  65. const int numThisTime = jmin (8192, numSamples);
  66. const int numBytes = numThisTime * sizeof (float);
  67. for (int c = (int) numChannels; --c >= 0;)
  68. {
  69. const int64 pos = sampleToReadPosition (c, startSampleInFile);
  70. input->setPosition (pos);
  71. if (destSamples[c] != nullptr)
  72. {
  73. if (c < (int) numChannels)
  74. input->read (destSamples[c] + startOffsetInDestBuffer, numBytes);
  75. else
  76. zeromem (destSamples[c] + startOffsetInDestBuffer, numBytes);
  77. }
  78. }
  79. startOffsetInDestBuffer += numThisTime;
  80. numSamples -= numThisTime;
  81. }
  82. return true;
  83. }
  84. bool ok;
  85. private:
  86. int64 sampleToReadPosition (int channel, int64 samplePosition)
  87. {
  88. const size_t startPosition = (numChannels + 1) * sizeof (float*);
  89. const int64 channelStartByte = startPosition + (channel * lengthInSamples * sizeof (float));
  90. const int64 sampleStartByte = channelStartByte + (samplePosition * sizeof (float));
  91. return sampleStartByte;
  92. }
  93. JUCE_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR (AudioSampleBufferReader);
  94. };
  95. //==============================================================================
  96. AudioSampleBufferAudioFormat::AudioSampleBufferAudioFormat()
  97. : AudioFormat (TRANS (audioSampleBufferAudioFormatName), StringArray())
  98. {
  99. }
  100. AudioSampleBufferAudioFormat::~AudioSampleBufferAudioFormat() {}
  101. Array<int> AudioSampleBufferAudioFormat::getPossibleSampleRates() { return Array<int>(); }
  102. Array<int> AudioSampleBufferAudioFormat::getPossibleBitDepths() { return Array<int>(); }
  103. bool AudioSampleBufferAudioFormat::canDoStereo() { return true; }
  104. bool AudioSampleBufferAudioFormat::canDoMono() { return true; }
  105. //==============================================================================
  106. AudioFormatReader* AudioSampleBufferAudioFormat::createReaderFor (InputStream* sourceStream,
  107. bool deleteStreamIfOpeningFails)
  108. {
  109. ScopedPointer<AudioSampleBufferReader> r (new AudioSampleBufferReader (sourceStream));
  110. if (r->ok)
  111. return r.release();
  112. if (! deleteStreamIfOpeningFails)
  113. r->input = nullptr;
  114. return nullptr;
  115. }
  116. AudioFormatWriter* AudioSampleBufferAudioFormat::createWriterFor (OutputStream* /*streamToWriteTo*/,
  117. double /*sampleRateToUse*/,
  118. unsigned int /*numberOfChannels*/,
  119. int /*bitsPerSample*/,
  120. const StringPairArray& /*metadataValues*/,
  121. int /*qualityOptionIndex*/)
  122. {
  123. jassertfalse; // not yet implemented!
  124. return nullptr;
  125. }