OLD | NEW |
1 /* | 1 /* |
2 * Copyright (C) 2010 Google Inc. All rights reserved. | 2 * Copyright (C) 2010 Google Inc. All rights reserved. |
3 * | 3 * |
4 * Redistribution and use in source and binary forms, with or without | 4 * Redistribution and use in source and binary forms, with or without |
5 * modification, are permitted provided that the following conditions | 5 * modification, are permitted provided that the following conditions |
6 * are met: | 6 * are met: |
7 * | 7 * |
8 * 1. Redistributions of source code must retain the above copyright | 8 * 1. Redistributions of source code must retain the above copyright |
9 * notice, this list of conditions and the following disclaimer. | 9 * notice, this list of conditions and the following disclaimer. |
10 * 2. Redistributions in binary form must reproduce the above copyright | 10 * 2. Redistributions in binary form must reproduce the above copyright |
(...skipping 36 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
47 namespace WebCore { | 47 namespace WebCore { |
48 | 48 |
49 using namespace VectorMath; | 49 using namespace VectorMath; |
50 | 50 |
51 const unsigned MaxBusChannels = 32; | 51 const unsigned MaxBusChannels = 32; |
52 | 52 |
53 PassRefPtr<AudioBus> AudioBus::create(unsigned numberOfChannels, size_t length,
bool allocate) | 53 PassRefPtr<AudioBus> AudioBus::create(unsigned numberOfChannels, size_t length,
bool allocate) |
54 { | 54 { |
55 ASSERT(numberOfChannels <= MaxBusChannels); | 55 ASSERT(numberOfChannels <= MaxBusChannels); |
56 if (numberOfChannels > MaxBusChannels) | 56 if (numberOfChannels > MaxBusChannels) |
57 return 0; | 57 return nullptr; |
58 | 58 |
59 return adoptRef(new AudioBus(numberOfChannels, length, allocate)); | 59 return adoptRef(new AudioBus(numberOfChannels, length, allocate)); |
60 } | 60 } |
61 | 61 |
62 AudioBus::AudioBus(unsigned numberOfChannels, size_t length, bool allocate) | 62 AudioBus::AudioBus(unsigned numberOfChannels, size_t length, bool allocate) |
63 : m_length(length) | 63 : m_length(length) |
64 , m_busGain(1) | 64 , m_busGain(1) |
65 , m_isFirstTime(true) | 65 , m_isFirstTime(true) |
66 , m_sampleRate(0) | 66 , m_sampleRate(0) |
67 { | 67 { |
(...skipping 105 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
173 | 173 |
174 PassRefPtr<AudioBus> AudioBus::createBufferFromRange(const AudioBus* sourceBuffe
r, unsigned startFrame, unsigned endFrame) | 174 PassRefPtr<AudioBus> AudioBus::createBufferFromRange(const AudioBus* sourceBuffe
r, unsigned startFrame, unsigned endFrame) |
175 { | 175 { |
176 size_t numberOfSourceFrames = sourceBuffer->length(); | 176 size_t numberOfSourceFrames = sourceBuffer->length(); |
177 unsigned numberOfChannels = sourceBuffer->numberOfChannels(); | 177 unsigned numberOfChannels = sourceBuffer->numberOfChannels(); |
178 | 178 |
179 // Sanity checking | 179 // Sanity checking |
180 bool isRangeSafe = startFrame < endFrame && endFrame <= numberOfSourceFrames
; | 180 bool isRangeSafe = startFrame < endFrame && endFrame <= numberOfSourceFrames
; |
181 ASSERT(isRangeSafe); | 181 ASSERT(isRangeSafe); |
182 if (!isRangeSafe) | 182 if (!isRangeSafe) |
183 return 0; | 183 return nullptr; |
184 | 184 |
185 size_t rangeLength = endFrame - startFrame; | 185 size_t rangeLength = endFrame - startFrame; |
186 | 186 |
187 RefPtr<AudioBus> audioBus = create(numberOfChannels, rangeLength); | 187 RefPtr<AudioBus> audioBus = create(numberOfChannels, rangeLength); |
188 audioBus->setSampleRate(sourceBuffer->sampleRate()); | 188 audioBus->setSampleRate(sourceBuffer->sampleRate()); |
189 | 189 |
190 for (unsigned i = 0; i < numberOfChannels; ++i) | 190 for (unsigned i = 0; i < numberOfChannels; ++i) |
191 audioBus->channel(i)->copyFromRange(sourceBuffer->channel(i), startFrame
, endFrame); | 191 audioBus->channel(i)->copyFromRange(sourceBuffer->channel(i), startFrame
, endFrame); |
192 | 192 |
193 return audioBus; | 193 return audioBus; |
(...skipping 329 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
523 float* destination = channel(channelIndex)->mutableData(); | 523 float* destination = channel(channelIndex)->mutableData(); |
524 vmul(source, 1, gainValues, 1, destination, 1, numberOfGainValues); | 524 vmul(source, 1, gainValues, 1, destination, 1, numberOfGainValues); |
525 } | 525 } |
526 } | 526 } |
527 | 527 |
528 PassRefPtr<AudioBus> AudioBus::createBySampleRateConverting(const AudioBus* sour
ceBus, bool mixToMono, double newSampleRate) | 528 PassRefPtr<AudioBus> AudioBus::createBySampleRateConverting(const AudioBus* sour
ceBus, bool mixToMono, double newSampleRate) |
529 { | 529 { |
530 // sourceBus's sample-rate must be known. | 530 // sourceBus's sample-rate must be known. |
531 ASSERT(sourceBus && sourceBus->sampleRate()); | 531 ASSERT(sourceBus && sourceBus->sampleRate()); |
532 if (!sourceBus || !sourceBus->sampleRate()) | 532 if (!sourceBus || !sourceBus->sampleRate()) |
533 return 0; | 533 return nullptr; |
534 | 534 |
535 double sourceSampleRate = sourceBus->sampleRate(); | 535 double sourceSampleRate = sourceBus->sampleRate(); |
536 double destinationSampleRate = newSampleRate; | 536 double destinationSampleRate = newSampleRate; |
537 double sampleRateRatio = sourceSampleRate / destinationSampleRate; | 537 double sampleRateRatio = sourceSampleRate / destinationSampleRate; |
538 unsigned numberOfSourceChannels = sourceBus->numberOfChannels(); | 538 unsigned numberOfSourceChannels = sourceBus->numberOfChannels(); |
539 | 539 |
540 if (numberOfSourceChannels == 1) | 540 if (numberOfSourceChannels == 1) |
541 mixToMono = false; // already mono | 541 mixToMono = false; // already mono |
542 | 542 |
543 if (sourceSampleRate == destinationSampleRate) { | 543 if (sourceSampleRate == destinationSampleRate) { |
(...skipping 66 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
610 for (unsigned i = 0; i < n; ++i) | 610 for (unsigned i = 0; i < n; ++i) |
611 destination[i] = (sourceL[i] + sourceR[i]) / 2; | 611 destination[i] = (sourceL[i] + sourceR[i]) / 2; |
612 | 612 |
613 destinationBus->clearSilentFlag(); | 613 destinationBus->clearSilentFlag(); |
614 destinationBus->setSampleRate(sourceBus->sampleRate()); | 614 destinationBus->setSampleRate(sourceBus->sampleRate()); |
615 return destinationBus; | 615 return destinationBus; |
616 } | 616 } |
617 } | 617 } |
618 | 618 |
619 ASSERT_NOT_REACHED(); | 619 ASSERT_NOT_REACHED(); |
620 return 0; | 620 return nullptr; |
621 } | 621 } |
622 | 622 |
623 bool AudioBus::isSilent() const | 623 bool AudioBus::isSilent() const |
624 { | 624 { |
625 for (size_t i = 0; i < m_channels.size(); ++i) { | 625 for (size_t i = 0; i < m_channels.size(); ++i) { |
626 if (!m_channels[i]->isSilent()) | 626 if (!m_channels[i]->isSilent()) |
627 return false; | 627 return false; |
628 } | 628 } |
629 return true; | 629 return true; |
630 } | 630 } |
631 | 631 |
632 void AudioBus::clearSilentFlag() | 632 void AudioBus::clearSilentFlag() |
633 { | 633 { |
634 for (size_t i = 0; i < m_channels.size(); ++i) | 634 for (size_t i = 0; i < m_channels.size(); ++i) |
635 m_channels[i]->clearSilentFlag(); | 635 m_channels[i]->clearSilentFlag(); |
636 } | 636 } |
637 | 637 |
638 PassRefPtr<AudioBus> decodeAudioFileData(const char* data, size_t size, double s
ampleRate) | 638 PassRefPtr<AudioBus> decodeAudioFileData(const char* data, size_t size, double s
ampleRate) |
639 { | 639 { |
640 blink::WebAudioBus webAudioBus; | 640 blink::WebAudioBus webAudioBus; |
641 if (blink::Platform::current()->loadAudioResource(&webAudioBus, data, size,
sampleRate)) | 641 if (blink::Platform::current()->loadAudioResource(&webAudioBus, data, size,
sampleRate)) |
642 return webAudioBus.release(); | 642 return webAudioBus.release(); |
643 return 0; | 643 return nullptr; |
644 } | 644 } |
645 | 645 |
646 PassRefPtr<AudioBus> AudioBus::loadPlatformResource(const char* name, float samp
leRate) | 646 PassRefPtr<AudioBus> AudioBus::loadPlatformResource(const char* name, float samp
leRate) |
647 { | 647 { |
648 const blink::WebData& resource = blink::Platform::current()->loadResource(na
me); | 648 const blink::WebData& resource = blink::Platform::current()->loadResource(na
me); |
649 if (resource.isEmpty()) | 649 if (resource.isEmpty()) |
650 return 0; | 650 return nullptr; |
651 | 651 |
652 // FIXME: the sampleRate parameter is ignored. It should be removed from the
API. | 652 // FIXME: the sampleRate parameter is ignored. It should be removed from the
API. |
653 RefPtr<AudioBus> audioBus = decodeAudioFileData(resource.data(), resource.si
ze(), sampleRate); | 653 RefPtr<AudioBus> audioBus = decodeAudioFileData(resource.data(), resource.si
ze(), sampleRate); |
654 | 654 |
655 if (!audioBus.get()) | 655 if (!audioBus.get()) |
656 return 0; | 656 return nullptr; |
657 | 657 |
658 // If the bus is already at the requested sample-rate then return as is. | 658 // If the bus is already at the requested sample-rate then return as is. |
659 if (audioBus->sampleRate() == sampleRate) | 659 if (audioBus->sampleRate() == sampleRate) |
660 return audioBus; | 660 return audioBus; |
661 | 661 |
662 return AudioBus::createBySampleRateConverting(audioBus.get(), false, sampleR
ate); | 662 return AudioBus::createBySampleRateConverting(audioBus.get(), false, sampleR
ate); |
663 } | 663 } |
664 | 664 |
665 PassRefPtr<AudioBus> createBusFromInMemoryAudioFile(const void* data, size_t dat
aSize, bool mixToMono, float sampleRate) | 665 PassRefPtr<AudioBus> createBusFromInMemoryAudioFile(const void* data, size_t dat
aSize, bool mixToMono, float sampleRate) |
666 { | 666 { |
667 // FIXME: the sampleRate parameter is ignored. It should be removed from the
API. | 667 // FIXME: the sampleRate parameter is ignored. It should be removed from the
API. |
668 RefPtr<AudioBus> audioBus = decodeAudioFileData(static_cast<const char*>(dat
a), dataSize, sampleRate); | 668 RefPtr<AudioBus> audioBus = decodeAudioFileData(static_cast<const char*>(dat
a), dataSize, sampleRate); |
669 if (!audioBus.get()) | 669 if (!audioBus.get()) |
670 return 0; | 670 return nullptr; |
671 | 671 |
672 // If the bus needs no conversion then return as is. | 672 // If the bus needs no conversion then return as is. |
673 if ((!mixToMono || audioBus->numberOfChannels() == 1) && audioBus->sampleRat
e() == sampleRate) | 673 if ((!mixToMono || audioBus->numberOfChannels() == 1) && audioBus->sampleRat
e() == sampleRate) |
674 return audioBus; | 674 return audioBus; |
675 | 675 |
676 return AudioBus::createBySampleRateConverting(audioBus.get(), mixToMono, sam
pleRate); | 676 return AudioBus::createBySampleRateConverting(audioBus.get(), mixToMono, sam
pleRate); |
677 } | 677 } |
678 | 678 |
679 } // WebCore | 679 } // WebCore |
680 | 680 |
681 #endif // ENABLE(WEB_AUDIO) | 681 #endif // ENABLE(WEB_AUDIO) |
OLD | NEW |