| 23 | namespace detail |
| 24 | { |
| 25 | std::shared_ptr<lab::AudioBus> LoadInternal(nqr::AudioData * audioData, bool mixToMono) |
| 26 | { |
| 27 | int numSamples = static_cast<int>(audioData->samples.size()); |
| 28 | if (!numSamples) return nullptr; |
| 29 | |
| 30 | int length = int(numSamples / audioData->channelCount); |
| 31 | const int busChannelCount = mixToMono ? 1 : (audioData->channelCount); |
| 32 | |
| 33 | std::vector<float> planarSamples(numSamples); |
| 34 | |
| 35 | // Create AudioBus where we'll put the PCM audio data |
| 36 | std::shared_ptr<lab::AudioBus> audioBus(new lab::AudioBus(busChannelCount, length)); |
| 37 | audioBus->setSampleRate((float) audioData->sampleRate); |
| 38 | |
| 39 | // Deinterleave stereo into LabSound/WebAudio planar channel layout |
| 40 | nqr::DeinterleaveChannels(audioData->samples.data(), planarSamples.data(), length, audioData->channelCount, length); |
| 41 | |
| 42 | // Mix to mono if stereo -- easier to do in place instead of using libnyquist helper functions |
| 43 | // because we've already deinterleaved |
| 44 | if (audioData->channelCount == lab::Channels::Stereo && mixToMono) |
| 45 | { |
| 46 | float * destinationMono = audioBus->channel(0)->mutableData(); |
| 47 | float * leftSamples = planarSamples.data(); |
| 48 | float * rightSamples = planarSamples.data() + length; |
| 49 | |
| 50 | for (int i = 0; i < length; i++) |
| 51 | { |
| 52 | destinationMono[i] = 0.5f * (leftSamples[i] + rightSamples[i]); |
| 53 | } |
| 54 | } |
| 55 | else |
| 56 | { |
| 57 | for (int i = 0; i < busChannelCount; ++i) |
| 58 | { |
| 59 | std::memcpy(audioBus->channel(i)->mutableData(), planarSamples.data() + (i * length), length * sizeof(float)); |
| 60 | } |
| 61 | } |
| 62 | |
| 63 | delete audioData; |
| 64 | |
| 65 | return audioBus; |
| 66 | } |
| 67 | } |
| 68 | |
| 69 | namespace lab |
no test coverage detected