2012-04-30 07:11:19 +04:00
|
|
|
/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
|
|
|
|
/* This Source Code Form is subject to the terms of the Mozilla Public
|
|
|
|
* License, v. 2.0. If a copy of the MPL was not distributed with this file,
|
|
|
|
* You can obtain one at http://mozilla.org/MPL/2.0/. */
|
|
|
|
|
|
|
|
#ifndef MOZILLA_AUDIOSEGMENT_H_
|
|
|
|
#define MOZILLA_AUDIOSEGMENT_H_
|
|
|
|
|
|
|
|
#include "MediaSegment.h"
|
2012-10-25 14:09:40 +04:00
|
|
|
#include "AudioSampleFormat.h"
|
2015-07-29 19:24:15 +03:00
|
|
|
#include "AudioChannelFormat.h"
|
2012-04-30 07:11:19 +04:00
|
|
|
#include "SharedBuffer.h"
|
2014-03-24 14:06:05 +04:00
|
|
|
#include "WebAudioUtils.h"
|
2013-01-28 22:22:37 +04:00
|
|
|
#ifdef MOZILLA_INTERNAL_API
|
|
|
|
#include "mozilla/TimeStamp.h"
|
|
|
|
#endif
|
2015-04-23 13:25:36 +03:00
|
|
|
#include <float.h>
|
2012-04-30 07:11:19 +04:00
|
|
|
|
|
|
|
namespace mozilla {
|
|
|
|
|
2014-03-24 14:06:05 +04:00
|
|
|
template<typename T>
|
|
|
|
class SharedChannelArrayBuffer : public ThreadSharedObject {
|
|
|
|
public:
|
2014-09-01 07:50:23 +04:00
|
|
|
explicit SharedChannelArrayBuffer(nsTArray<nsTArray<T> >* aBuffers)
|
2014-03-24 14:06:05 +04:00
|
|
|
{
|
|
|
|
mBuffers.SwapElements(*aBuffers);
|
|
|
|
}
|
2014-04-13 22:08:10 +04:00
|
|
|
|
2016-01-18 06:50:29 +03:00
|
|
|
size_t SizeOfExcludingThis(MallocSizeOf aMallocSizeOf) const override
|
2014-04-13 22:08:10 +04:00
|
|
|
{
|
|
|
|
size_t amount = 0;
|
2015-07-29 09:24:24 +03:00
|
|
|
amount += mBuffers.ShallowSizeOfExcludingThis(aMallocSizeOf);
|
2014-04-13 22:08:10 +04:00
|
|
|
for (size_t i = 0; i < mBuffers.Length(); i++) {
|
2015-07-29 09:24:24 +03:00
|
|
|
amount += mBuffers[i].ShallowSizeOfExcludingThis(aMallocSizeOf);
|
2014-04-13 22:08:10 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
return amount;
|
|
|
|
}
|
|
|
|
|
2016-01-18 06:50:29 +03:00
|
|
|
size_t SizeOfIncludingThis(MallocSizeOf aMallocSizeOf) const override
|
2014-04-13 22:08:10 +04:00
|
|
|
{
|
|
|
|
return aMallocSizeOf(this) + SizeOfExcludingThis(aMallocSizeOf);
|
|
|
|
}
|
|
|
|
|
|
|
|
nsTArray<nsTArray<T> > mBuffers;
|
2014-03-24 14:06:05 +04:00
|
|
|
};
|
|
|
|
|
2014-03-24 14:06:06 +04:00
|
|
|
class AudioMixer;
|
2012-11-14 23:45:33 +04:00
|
|
|
|
2013-01-14 02:46:57 +04:00
|
|
|
/**
|
|
|
|
* For auto-arrays etc, guess this as the common number of channels.
|
|
|
|
*/
|
|
|
|
const int GUESS_AUDIO_CHANNELS = 2;
|
|
|
|
|
2013-05-02 07:12:59 +04:00
|
|
|
// We ensure that the graph advances in steps that are multiples of the Web
|
|
|
|
// Audio block size
|
|
|
|
const uint32_t WEBAUDIO_BLOCK_SIZE_BITS = 7;
|
|
|
|
const uint32_t WEBAUDIO_BLOCK_SIZE = 1 << WEBAUDIO_BLOCK_SIZE_BITS;
|
|
|
|
|
2015-09-01 15:25:48 +03:00
|
|
|
template <typename SrcT, typename DestT>
|
2015-07-29 19:24:15 +03:00
|
|
|
static void
|
|
|
|
InterleaveAndConvertBuffer(const SrcT* const* aSourceChannels,
|
2015-09-01 15:25:48 +03:00
|
|
|
uint32_t aLength, float aVolume,
|
|
|
|
uint32_t aChannels,
|
2015-07-29 19:24:15 +03:00
|
|
|
DestT* aOutput)
|
|
|
|
{
|
|
|
|
DestT* output = aOutput;
|
2015-09-01 15:25:48 +03:00
|
|
|
for (size_t i = 0; i < aLength; ++i) {
|
|
|
|
for (size_t channel = 0; channel < aChannels; ++channel) {
|
2015-07-29 19:24:15 +03:00
|
|
|
float v = AudioSampleToFloat(aSourceChannels[channel][i])*aVolume;
|
|
|
|
*output = FloatToAudioSample<DestT>(v);
|
|
|
|
++output;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-09-01 15:25:48 +03:00
|
|
|
template <typename SrcT, typename DestT>
|
|
|
|
static void
|
|
|
|
DeinterleaveAndConvertBuffer(const SrcT* aSourceBuffer,
|
|
|
|
uint32_t aFrames, uint32_t aChannels,
|
|
|
|
DestT** aOutput)
|
|
|
|
{
|
|
|
|
for (size_t i = 0; i < aChannels; i++) {
|
|
|
|
size_t interleavedIndex = i;
|
|
|
|
for (size_t j = 0; j < aFrames; j++) {
|
|
|
|
ConvertAudioSample(aSourceBuffer[interleavedIndex],
|
|
|
|
aOutput[i][j]);
|
|
|
|
interleavedIndex += aChannels;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-07-29 19:24:15 +03:00
|
|
|
class SilentChannel
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
static const int AUDIO_PROCESSING_FRAMES = 640; /* > 10ms of 48KHz audio */
|
|
|
|
static const uint8_t gZeroChannel[MAX_AUDIO_SAMPLE_SIZE*AUDIO_PROCESSING_FRAMES];
|
|
|
|
// We take advantage of the fact that zero in float and zero in int have the
|
|
|
|
// same all-zeros bit layout.
|
|
|
|
template<typename T>
|
|
|
|
static const T* ZeroChannel();
|
|
|
|
};
|
|
|
|
|
2013-06-14 11:16:41 +04:00
|
|
|
|
2013-06-03 13:59:50 +04:00
|
|
|
/**
|
2013-06-14 11:16:41 +04:00
|
|
|
* Given an array of input channels (aChannelData), downmix to aOutputChannels,
|
|
|
|
* interleave the channel data. A total of aOutputChannels*aDuration
|
|
|
|
* interleaved samples will be copied to a channel buffer in aOutput.
|
2013-06-03 13:59:50 +04:00
|
|
|
*/
|
2015-07-29 19:24:15 +03:00
|
|
|
template <typename SrcT, typename DestT>
|
|
|
|
void
|
|
|
|
DownmixAndInterleave(const nsTArray<const SrcT*>& aChannelData,
|
|
|
|
int32_t aDuration, float aVolume, uint32_t aOutputChannels,
|
|
|
|
DestT* aOutput)
|
|
|
|
{
|
|
|
|
|
|
|
|
if (aChannelData.Length() == aOutputChannels) {
|
|
|
|
InterleaveAndConvertBuffer(aChannelData.Elements(),
|
|
|
|
aDuration, aVolume, aOutputChannels, aOutput);
|
|
|
|
} else {
|
2016-02-02 18:36:30 +03:00
|
|
|
AutoTArray<SrcT*,GUESS_AUDIO_CHANNELS> outputChannelData;
|
|
|
|
AutoTArray<SrcT, SilentChannel::AUDIO_PROCESSING_FRAMES * GUESS_AUDIO_CHANNELS> outputBuffers;
|
2015-07-29 19:24:15 +03:00
|
|
|
outputChannelData.SetLength(aOutputChannels);
|
|
|
|
outputBuffers.SetLength(aDuration * aOutputChannels);
|
|
|
|
for (uint32_t i = 0; i < aOutputChannels; i++) {
|
|
|
|
outputChannelData[i] = outputBuffers.Elements() + aDuration * i;
|
|
|
|
}
|
|
|
|
AudioChannelsDownMix(aChannelData,
|
|
|
|
outputChannelData.Elements(),
|
|
|
|
aOutputChannels,
|
|
|
|
aDuration);
|
|
|
|
InterleaveAndConvertBuffer(outputChannelData.Elements(),
|
|
|
|
aDuration, aVolume, aOutputChannels, aOutput);
|
|
|
|
}
|
|
|
|
}
|
2013-06-03 13:59:50 +04:00
|
|
|
|
2012-11-22 09:04:27 +04:00
|
|
|
/**
|
|
|
|
* An AudioChunk represents a multi-channel buffer of audio samples.
|
|
|
|
* It references an underlying ThreadSharedObject which manages the lifetime
|
|
|
|
* of the buffer. An AudioChunk maintains its own duration and channel data
|
|
|
|
* pointers so it can represent a subinterval of a buffer without copying.
|
|
|
|
* An AudioChunk can store its individual channels anywhere; it maintains
|
|
|
|
* separate pointers to each channel's buffer.
|
|
|
|
*/
|
2012-04-30 07:11:19 +04:00
|
|
|
struct AudioChunk {
|
2012-10-25 14:09:40 +04:00
|
|
|
typedef mozilla::AudioSampleFormat SampleFormat;
|
2012-04-30 07:11:19 +04:00
|
|
|
|
2016-04-06 15:56:44 +03:00
|
|
|
AudioChunk() : mPrincipalHandle(PRINCIPAL_HANDLE_NONE) {}
|
|
|
|
|
2012-04-30 07:11:19 +04:00
|
|
|
// Generic methods
|
2014-09-18 09:20:43 +04:00
|
|
|
void SliceTo(StreamTime aStart, StreamTime aEnd)
|
2012-04-30 07:11:19 +04:00
|
|
|
{
|
2014-09-26 22:13:17 +04:00
|
|
|
MOZ_ASSERT(aStart >= 0 && aStart < aEnd && aEnd <= mDuration,
|
|
|
|
"Slice out of bounds");
|
2012-04-30 07:11:19 +04:00
|
|
|
if (mBuffer) {
|
2012-11-22 09:04:27 +04:00
|
|
|
MOZ_ASSERT(aStart < INT32_MAX, "Can't slice beyond 32-bit sample lengths");
|
|
|
|
for (uint32_t channel = 0; channel < mChannelData.Length(); ++channel) {
|
|
|
|
mChannelData[channel] = AddAudioSampleOffset(mChannelData[channel],
|
|
|
|
mBufferFormat, int32_t(aStart));
|
|
|
|
}
|
2012-04-30 07:11:19 +04:00
|
|
|
}
|
|
|
|
mDuration = aEnd - aStart;
|
|
|
|
}
|
2014-09-18 09:20:43 +04:00
|
|
|
StreamTime GetDuration() const { return mDuration; }
|
2012-04-30 07:11:19 +04:00
|
|
|
bool CanCombineWithFollowing(const AudioChunk& aOther) const
|
|
|
|
{
|
|
|
|
if (aOther.mBuffer != mBuffer) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
if (mBuffer) {
|
2012-11-22 09:04:27 +04:00
|
|
|
NS_ASSERTION(aOther.mBufferFormat == mBufferFormat,
|
2012-04-30 07:11:19 +04:00
|
|
|
"Wrong metadata about buffer");
|
2012-11-22 09:04:27 +04:00
|
|
|
NS_ASSERTION(aOther.mChannelData.Length() == mChannelData.Length(),
|
|
|
|
"Mismatched channel count");
|
|
|
|
if (mDuration > INT32_MAX) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
for (uint32_t channel = 0; channel < mChannelData.Length(); ++channel) {
|
|
|
|
if (aOther.mChannelData[channel] != AddAudioSampleOffset(mChannelData[channel],
|
|
|
|
mBufferFormat, int32_t(mDuration))) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
2012-04-30 07:11:19 +04:00
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
2012-07-30 18:20:58 +04:00
|
|
|
bool IsNull() const { return mBuffer == nullptr; }
|
2014-09-18 09:20:43 +04:00
|
|
|
void SetNull(StreamTime aDuration)
|
2012-04-30 07:11:19 +04:00
|
|
|
{
|
2012-07-30 18:20:58 +04:00
|
|
|
mBuffer = nullptr;
|
2012-11-22 09:04:27 +04:00
|
|
|
mChannelData.Clear();
|
2012-04-30 07:11:19 +04:00
|
|
|
mDuration = aDuration;
|
|
|
|
mVolume = 1.0f;
|
2014-04-07 20:22:11 +04:00
|
|
|
mBufferFormat = AUDIO_FORMAT_SILENCE;
|
2016-04-06 15:56:44 +03:00
|
|
|
mPrincipalHandle = PRINCIPAL_HANDLE_NONE;
|
2012-04-30 07:11:19 +04:00
|
|
|
}
|
2015-04-23 13:25:36 +03:00
|
|
|
|
2015-07-22 09:51:11 +03:00
|
|
|
size_t ChannelCount() const { return mChannelData.Length(); }
|
2012-04-30 07:11:19 +04:00
|
|
|
|
2014-06-25 09:15:36 +04:00
|
|
|
bool IsMuted() const { return mVolume == 0.0f; }
|
|
|
|
|
2014-04-13 22:08:10 +04:00
|
|
|
size_t SizeOfExcludingThisIfUnshared(MallocSizeOf aMallocSizeOf) const
|
|
|
|
{
|
|
|
|
return SizeOfExcludingThis(aMallocSizeOf, true);
|
|
|
|
}
|
|
|
|
|
|
|
|
size_t SizeOfExcludingThis(MallocSizeOf aMallocSizeOf, bool aUnshared) const
|
|
|
|
{
|
|
|
|
size_t amount = 0;
|
|
|
|
|
|
|
|
// Possibly owned:
|
|
|
|
// - mBuffer - Can hold data that is also in the decoded audio queue. If it
|
|
|
|
// is not shared, or unshared == false it gets counted.
|
|
|
|
if (mBuffer && (!aUnshared || !mBuffer->IsShared())) {
|
|
|
|
amount += mBuffer->SizeOfIncludingThis(aMallocSizeOf);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Memory in the array is owned by mBuffer.
|
2015-07-29 09:24:24 +03:00
|
|
|
amount += mChannelData.ShallowSizeOfExcludingThis(aMallocSizeOf);
|
2014-04-13 22:08:10 +04:00
|
|
|
return amount;
|
|
|
|
}
|
|
|
|
|
2015-07-29 19:24:15 +03:00
|
|
|
template<typename T>
|
|
|
|
const nsTArray<const T*>& ChannelData()
|
|
|
|
{
|
|
|
|
MOZ_ASSERT(AudioSampleTypeToFormat<T>::Format == mBufferFormat);
|
|
|
|
return *reinterpret_cast<nsTArray<const T*>*>(&mChannelData);
|
|
|
|
}
|
|
|
|
|
2016-04-06 15:56:44 +03:00
|
|
|
PrincipalHandle GetPrincipalHandle() const { return mPrincipalHandle; }
|
|
|
|
|
2014-09-18 09:20:43 +04:00
|
|
|
StreamTime mDuration; // in frames within the buffer
|
2015-10-18 08:24:48 +03:00
|
|
|
RefPtr<ThreadSharedObject> mBuffer; // the buffer object whose lifetime is managed; null means data is all zeroes
|
2012-11-22 09:04:27 +04:00
|
|
|
nsTArray<const void*> mChannelData; // one pointer per channel; empty if and only if mBuffer is null
|
|
|
|
float mVolume; // volume multiplier to apply (1.0f if mBuffer is nonnull)
|
|
|
|
SampleFormat mBufferFormat; // format of frames in mBuffer (only meaningful if mBuffer is nonnull)
|
2013-01-28 22:22:37 +04:00
|
|
|
#ifdef MOZILLA_INTERNAL_API
|
|
|
|
mozilla::TimeStamp mTimeStamp; // time at which this has been fetched from the MediaEngine
|
|
|
|
#endif
|
2016-04-06 15:56:44 +03:00
|
|
|
// principalHandle for the data in this chunk.
|
|
|
|
// This can be compared to an nsIPrincipal* when back on main thread.
|
|
|
|
PrincipalHandle mPrincipalHandle;
|
2012-04-30 07:11:19 +04:00
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* A list of audio samples consisting of a sequence of slices of SharedBuffers.
|
|
|
|
* The audio rate is determined by the track, not stored in this class.
|
|
|
|
*/
|
|
|
|
class AudioSegment : public MediaSegmentBase<AudioSegment, AudioChunk> {
|
|
|
|
public:
|
2012-10-25 14:09:40 +04:00
|
|
|
typedef mozilla::AudioSampleFormat SampleFormat;
|
2012-04-30 07:11:19 +04:00
|
|
|
|
2013-01-21 00:44:44 +04:00
|
|
|
AudioSegment() : MediaSegmentBase<AudioSegment, AudioChunk>(AUDIO) {}
|
2012-04-30 07:11:19 +04:00
|
|
|
|
2014-03-24 14:06:05 +04:00
|
|
|
// Resample the whole segment in place.
|
|
|
|
template<typename T>
|
|
|
|
void Resample(SpeexResamplerState* aResampler, uint32_t aInRate, uint32_t aOutRate)
|
|
|
|
{
|
|
|
|
mDuration = 0;
|
2014-06-09 04:11:41 +04:00
|
|
|
#ifdef DEBUG
|
|
|
|
uint32_t segmentChannelCount = ChannelCount();
|
|
|
|
#endif
|
2014-03-24 14:06:05 +04:00
|
|
|
|
|
|
|
for (ChunkIterator ci(*this); !ci.IsEnded(); ci.Next()) {
|
2016-02-02 18:36:30 +03:00
|
|
|
AutoTArray<nsTArray<T>, GUESS_AUDIO_CHANNELS> output;
|
|
|
|
AutoTArray<const T*, GUESS_AUDIO_CHANNELS> bufferPtrs;
|
2014-03-24 14:06:05 +04:00
|
|
|
AudioChunk& c = *ci;
|
2014-04-07 20:22:11 +04:00
|
|
|
// If this chunk is null, don't bother resampling, just alter its duration
|
|
|
|
if (c.IsNull()) {
|
2014-06-10 06:32:30 +04:00
|
|
|
c.mDuration = (c.mDuration * aOutRate) / aInRate;
|
2014-04-07 20:22:11 +04:00
|
|
|
mDuration += c.mDuration;
|
2014-04-25 17:15:21 +04:00
|
|
|
continue;
|
2014-04-07 20:22:11 +04:00
|
|
|
}
|
2014-03-24 14:06:05 +04:00
|
|
|
uint32_t channels = c.mChannelData.Length();
|
2014-06-09 04:11:41 +04:00
|
|
|
MOZ_ASSERT(channels == segmentChannelCount);
|
2014-03-24 14:06:05 +04:00
|
|
|
output.SetLength(channels);
|
|
|
|
bufferPtrs.SetLength(channels);
|
2014-06-09 04:11:08 +04:00
|
|
|
uint32_t inFrames = c.mDuration;
|
|
|
|
// Round up to allocate; the last frame may not be used.
|
|
|
|
NS_ASSERTION((UINT32_MAX - aInRate + 1) / c.mDuration >= aOutRate,
|
|
|
|
"Dropping samples");
|
|
|
|
uint32_t outSize = (c.mDuration * aOutRate + aInRate - 1) / aInRate;
|
2014-03-24 14:06:05 +04:00
|
|
|
for (uint32_t i = 0; i < channels; i++) {
|
2014-06-09 04:11:08 +04:00
|
|
|
T* out = output[i].AppendElements(outSize);
|
|
|
|
uint32_t outFrames = outSize;
|
2014-03-24 14:06:05 +04:00
|
|
|
|
2015-04-10 21:54:24 +03:00
|
|
|
const T* in = static_cast<const T*>(c.mChannelData[i]);
|
2014-03-24 14:06:05 +04:00
|
|
|
dom::WebAudioUtils::SpeexResamplerProcess(aResampler, i,
|
|
|
|
in, &inFrames,
|
|
|
|
out, &outFrames);
|
2014-06-09 04:11:08 +04:00
|
|
|
MOZ_ASSERT(inFrames == c.mDuration);
|
2015-04-10 21:54:24 +03:00
|
|
|
|
2014-03-24 14:06:05 +04:00
|
|
|
bufferPtrs[i] = out;
|
|
|
|
output[i].SetLength(outFrames);
|
|
|
|
}
|
2014-06-09 04:11:08 +04:00
|
|
|
MOZ_ASSERT(channels > 0);
|
|
|
|
c.mDuration = output[0].Length();
|
2014-03-24 14:06:05 +04:00
|
|
|
c.mBuffer = new mozilla::SharedChannelArrayBuffer<T>(&output);
|
|
|
|
for (uint32_t i = 0; i < channels; i++) {
|
|
|
|
c.mChannelData[i] = bufferPtrs[i];
|
|
|
|
}
|
|
|
|
mDuration += c.mDuration;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-06-26 16:01:01 +04:00
|
|
|
void ResampleChunks(SpeexResamplerState* aResampler,
|
|
|
|
uint32_t aInRate,
|
|
|
|
uint32_t aOutRate);
|
2014-03-24 14:06:05 +04:00
|
|
|
|
2012-11-22 09:04:27 +04:00
|
|
|
void AppendFrames(already_AddRefed<ThreadSharedObject> aBuffer,
|
|
|
|
const nsTArray<const float*>& aChannelData,
|
2016-04-06 15:56:44 +03:00
|
|
|
int32_t aDuration, const PrincipalHandle& aPrincipalHandle)
|
2012-04-30 07:11:19 +04:00
|
|
|
{
|
2012-11-22 09:04:27 +04:00
|
|
|
AudioChunk* chunk = AppendChunk(aDuration);
|
2012-04-30 07:11:19 +04:00
|
|
|
chunk->mBuffer = aBuffer;
|
2012-11-22 09:04:27 +04:00
|
|
|
for (uint32_t channel = 0; channel < aChannelData.Length(); ++channel) {
|
|
|
|
chunk->mChannelData.AppendElement(aChannelData[channel]);
|
|
|
|
}
|
|
|
|
chunk->mVolume = 1.0f;
|
|
|
|
chunk->mBufferFormat = AUDIO_FORMAT_FLOAT32;
|
2013-01-28 22:22:37 +04:00
|
|
|
#ifdef MOZILLA_INTERNAL_API
|
|
|
|
chunk->mTimeStamp = TimeStamp::Now();
|
|
|
|
#endif
|
2016-04-06 15:56:44 +03:00
|
|
|
chunk->mPrincipalHandle = aPrincipalHandle;
|
2012-11-22 09:04:27 +04:00
|
|
|
}
|
|
|
|
void AppendFrames(already_AddRefed<ThreadSharedObject> aBuffer,
|
|
|
|
const nsTArray<const int16_t*>& aChannelData,
|
2016-04-06 15:56:44 +03:00
|
|
|
int32_t aDuration, const PrincipalHandle& aPrincipalHandle)
|
2012-11-22 09:04:27 +04:00
|
|
|
{
|
|
|
|
AudioChunk* chunk = AppendChunk(aDuration);
|
|
|
|
chunk->mBuffer = aBuffer;
|
|
|
|
for (uint32_t channel = 0; channel < aChannelData.Length(); ++channel) {
|
|
|
|
chunk->mChannelData.AppendElement(aChannelData[channel]);
|
|
|
|
}
|
2012-04-30 07:11:19 +04:00
|
|
|
chunk->mVolume = 1.0f;
|
2012-11-22 09:04:27 +04:00
|
|
|
chunk->mBufferFormat = AUDIO_FORMAT_S16;
|
2013-01-28 22:22:37 +04:00
|
|
|
#ifdef MOZILLA_INTERNAL_API
|
|
|
|
chunk->mTimeStamp = TimeStamp::Now();
|
|
|
|
#endif
|
2016-04-06 15:56:44 +03:00
|
|
|
chunk->mPrincipalHandle = aPrincipalHandle;
|
2012-04-30 07:11:19 +04:00
|
|
|
}
|
2013-01-21 00:44:44 +04:00
|
|
|
// Consumes aChunk, and returns a pointer to the persistent copy of aChunk
|
|
|
|
// in the segment.
|
|
|
|
AudioChunk* AppendAndConsumeChunk(AudioChunk* aChunk)
|
|
|
|
{
|
|
|
|
AudioChunk* chunk = AppendChunk(aChunk->mDuration);
|
|
|
|
chunk->mBuffer = aChunk->mBuffer.forget();
|
|
|
|
chunk->mChannelData.SwapElements(aChunk->mChannelData);
|
|
|
|
chunk->mVolume = aChunk->mVolume;
|
|
|
|
chunk->mBufferFormat = aChunk->mBufferFormat;
|
2013-01-28 22:22:37 +04:00
|
|
|
#ifdef MOZILLA_INTERNAL_API
|
|
|
|
chunk->mTimeStamp = TimeStamp::Now();
|
|
|
|
#endif
|
2016-04-06 15:56:44 +03:00
|
|
|
chunk->mPrincipalHandle = aChunk->mPrincipalHandle;
|
2013-01-21 00:44:44 +04:00
|
|
|
return chunk;
|
|
|
|
}
|
2012-04-30 07:11:19 +04:00
|
|
|
void ApplyVolume(float aVolume);
|
2015-07-24 15:28:16 +03:00
|
|
|
// Mix the segment into a mixer, interleaved. This is useful to output a
|
|
|
|
// segment to a system audio callback. It up or down mixes to aChannelCount
|
|
|
|
// channels.
|
|
|
|
void WriteTo(uint64_t aID, AudioMixer& aMixer, uint32_t aChannelCount,
|
|
|
|
uint32_t aSampleRate);
|
|
|
|
// Mix the segment into a mixer, keeping it planar, up or down mixing to
|
|
|
|
// aChannelCount channels.
|
|
|
|
void Mix(AudioMixer& aMixer, uint32_t aChannelCount, uint32_t aSampleRate);
|
2014-03-24 14:06:05 +04:00
|
|
|
|
2014-03-24 14:06:05 +04:00
|
|
|
int ChannelCount() {
|
2016-09-01 08:01:16 +03:00
|
|
|
NS_WARNING_ASSERTION(
|
|
|
|
!mChunks.IsEmpty(),
|
|
|
|
"Cannot query channel count on a AudioSegment with no chunks.");
|
2014-04-25 17:15:21 +04:00
|
|
|
// Find the first chunk that has non-zero channels. A chunk that hs zero
|
|
|
|
// channels is just silence and we can simply discard it.
|
|
|
|
for (ChunkIterator ci(*this); !ci.IsEnded(); ci.Next()) {
|
|
|
|
if (ci->ChannelCount()) {
|
|
|
|
return ci->ChannelCount();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return 0;
|
2014-03-24 14:06:05 +04:00
|
|
|
}
|
|
|
|
|
2014-09-18 03:51:21 +04:00
|
|
|
bool IsNull() const {
|
|
|
|
for (ChunkIterator ci(*const_cast<AudioSegment*>(this)); !ci.IsEnded();
|
|
|
|
ci.Next()) {
|
2014-06-26 16:01:01 +04:00
|
|
|
if (!ci->IsNull()) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2012-04-30 07:11:19 +04:00
|
|
|
static Type StaticType() { return AUDIO; }
|
2014-04-13 22:08:10 +04:00
|
|
|
|
2016-01-18 06:50:29 +03:00
|
|
|
size_t SizeOfIncludingThis(MallocSizeOf aMallocSizeOf) const override
|
2014-04-13 22:08:10 +04:00
|
|
|
{
|
|
|
|
return aMallocSizeOf(this) + SizeOfExcludingThis(aMallocSizeOf);
|
|
|
|
}
|
2012-04-30 07:11:19 +04:00
|
|
|
};
|
|
|
|
|
2015-07-29 19:24:15 +03:00
|
|
|
template<typename SrcT>
|
|
|
|
void WriteChunk(AudioChunk& aChunk,
|
|
|
|
uint32_t aOutputChannels,
|
|
|
|
AudioDataValue* aOutputBuffer)
|
|
|
|
{
|
2016-02-02 18:36:30 +03:00
|
|
|
AutoTArray<const SrcT*,GUESS_AUDIO_CHANNELS> channelData;
|
2015-07-29 19:24:15 +03:00
|
|
|
|
|
|
|
channelData = aChunk.ChannelData<SrcT>();
|
|
|
|
|
|
|
|
if (channelData.Length() < aOutputChannels) {
|
|
|
|
// Up-mix. Note that this might actually make channelData have more
|
|
|
|
// than aOutputChannels temporarily.
|
|
|
|
AudioChannelsUpMix(&channelData, aOutputChannels, SilentChannel::ZeroChannel<SrcT>());
|
|
|
|
}
|
|
|
|
if (channelData.Length() > aOutputChannels) {
|
|
|
|
// Down-mix.
|
|
|
|
DownmixAndInterleave(channelData, aChunk.mDuration,
|
|
|
|
aChunk.mVolume, aOutputChannels, aOutputBuffer);
|
|
|
|
} else {
|
|
|
|
InterleaveAndConvertBuffer(channelData.Elements(),
|
|
|
|
aChunk.mDuration, aChunk.mVolume,
|
|
|
|
aOutputChannels,
|
|
|
|
aOutputBuffer);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
2015-07-13 18:25:42 +03:00
|
|
|
} // namespace mozilla
|
2012-04-30 07:11:19 +04:00
|
|
|
|
|
|
|
#endif /* MOZILLA_AUDIOSEGMENT_H_ */
|