2012-04-29 20:11:19 -07: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 03:09:40 -07:00
|
|
|
#include "AudioSampleFormat.h"
|
2012-04-29 20:11:19 -07:00
|
|
|
#include "SharedBuffer.h"
|
2014-03-24 03:06:05 -07:00
|
|
|
#include "WebAudioUtils.h"
|
2013-01-28 10:22:37 -08:00
|
|
|
#ifdef MOZILLA_INTERNAL_API
|
|
|
|
#include "mozilla/TimeStamp.h"
|
|
|
|
#endif
|
2012-04-29 20:11:19 -07:00
|
|
|
|
|
|
|
namespace mozilla {
|
|
|
|
|
2014-03-24 03:06:05 -07:00
|
|
|
template<typename T>
|
|
|
|
class SharedChannelArrayBuffer : public ThreadSharedObject {
|
|
|
|
public:
|
|
|
|
SharedChannelArrayBuffer(nsTArray<nsTArray<T>>* aBuffers)
|
|
|
|
{
|
|
|
|
mBuffers.SwapElements(*aBuffers);
|
|
|
|
}
|
|
|
|
nsTArray<nsTArray<T>> mBuffers;
|
|
|
|
};
|
|
|
|
|
2012-11-14 11:46:40 -08:00
|
|
|
class AudioStream;
|
2014-03-24 03:06:06 -07:00
|
|
|
class AudioMixer;
|
2012-11-14 11:45:33 -08:00
|
|
|
|
2013-01-13 14:46:57 -08:00
|
|
|
/**
|
|
|
|
* For auto-arrays etc, guess this as the common number of channels.
|
|
|
|
*/
|
|
|
|
const int GUESS_AUDIO_CHANNELS = 2;
|
|
|
|
|
2013-05-01 20:12:59 -07: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;
|
|
|
|
|
2013-06-03 02:59:50 -07:00
|
|
|
void InterleaveAndConvertBuffer(const void** aSourceChannels,
|
|
|
|
AudioSampleFormat aSourceFormat,
|
|
|
|
int32_t aLength, float aVolume,
|
|
|
|
int32_t aChannels,
|
|
|
|
AudioDataValue* aOutput);
|
2013-06-14 00:16:41 -07:00
|
|
|
|
2013-06-03 02:59:50 -07:00
|
|
|
/**
|
2013-06-14 00:16:41 -07: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 02:59:50 -07:00
|
|
|
*/
|
|
|
|
void DownmixAndInterleave(const nsTArray<const void*>& aChannelData,
|
|
|
|
AudioSampleFormat aSourceFormat, int32_t aDuration,
|
2013-06-11 14:50:21 -07:00
|
|
|
float aVolume, uint32_t aOutputChannels,
|
2013-06-03 02:59:50 -07:00
|
|
|
AudioDataValue* aOutput);
|
|
|
|
|
2012-11-21 21:04:27 -08: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-29 20:11:19 -07:00
|
|
|
struct AudioChunk {
|
2012-10-25 03:09:40 -07:00
|
|
|
typedef mozilla::AudioSampleFormat SampleFormat;
|
2012-04-29 20:11:19 -07:00
|
|
|
|
|
|
|
// Generic methods
|
|
|
|
void SliceTo(TrackTicks aStart, TrackTicks aEnd)
|
|
|
|
{
|
|
|
|
NS_ASSERTION(aStart >= 0 && aStart < aEnd && aEnd <= mDuration,
|
|
|
|
"Slice out of bounds");
|
|
|
|
if (mBuffer) {
|
2012-11-21 21:04:27 -08: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-29 20:11:19 -07:00
|
|
|
}
|
|
|
|
mDuration = aEnd - aStart;
|
|
|
|
}
|
|
|
|
TrackTicks GetDuration() const { return mDuration; }
|
|
|
|
bool CanCombineWithFollowing(const AudioChunk& aOther) const
|
|
|
|
{
|
|
|
|
if (aOther.mBuffer != mBuffer) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
if (mBuffer) {
|
2012-11-21 21:04:27 -08:00
|
|
|
NS_ASSERTION(aOther.mBufferFormat == mBufferFormat,
|
2012-04-29 20:11:19 -07:00
|
|
|
"Wrong metadata about buffer");
|
2012-11-21 21:04:27 -08: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-29 20:11:19 -07:00
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
2012-07-30 07:20:58 -07:00
|
|
|
bool IsNull() const { return mBuffer == nullptr; }
|
2012-04-29 20:11:19 -07:00
|
|
|
void SetNull(TrackTicks aDuration)
|
|
|
|
{
|
2012-07-30 07:20:58 -07:00
|
|
|
mBuffer = nullptr;
|
2012-11-21 21:04:27 -08:00
|
|
|
mChannelData.Clear();
|
2012-04-29 20:11:19 -07:00
|
|
|
mDuration = aDuration;
|
|
|
|
mVolume = 1.0f;
|
|
|
|
}
|
2014-03-04 13:06:57 -08:00
|
|
|
int ChannelCount() const { return mChannelData.Length(); }
|
2012-04-29 20:11:19 -07:00
|
|
|
|
2012-11-21 21:04:27 -08:00
|
|
|
TrackTicks mDuration; // in frames within the buffer
|
|
|
|
nsRefPtr<ThreadSharedObject> mBuffer; // the buffer object whose lifetime is managed; null means data is all zeroes
|
|
|
|
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 10:22:37 -08:00
|
|
|
#ifdef MOZILLA_INTERNAL_API
|
|
|
|
mozilla::TimeStamp mTimeStamp; // time at which this has been fetched from the MediaEngine
|
|
|
|
#endif
|
2012-04-29 20:11:19 -07:00
|
|
|
};
|
|
|
|
|
2014-03-24 03:06:05 -07:00
|
|
|
|
2012-04-29 20:11:19 -07: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 03:09:40 -07:00
|
|
|
typedef mozilla::AudioSampleFormat SampleFormat;
|
2012-04-29 20:11:19 -07:00
|
|
|
|
2013-01-20 12:44:44 -08:00
|
|
|
AudioSegment() : MediaSegmentBase<AudioSegment, AudioChunk>(AUDIO) {}
|
2012-04-29 20:11:19 -07:00
|
|
|
|
2014-03-24 03:06:05 -07:00
|
|
|
// Resample the whole segment in place.
|
|
|
|
template<typename T>
|
|
|
|
void Resample(SpeexResamplerState* aResampler, uint32_t aInRate, uint32_t aOutRate)
|
|
|
|
{
|
|
|
|
mDuration = 0;
|
|
|
|
|
|
|
|
for (ChunkIterator ci(*this); !ci.IsEnded(); ci.Next()) {
|
|
|
|
nsAutoTArray<nsTArray<T>, GUESS_AUDIO_CHANNELS> output;
|
|
|
|
nsAutoTArray<const T*, GUESS_AUDIO_CHANNELS> bufferPtrs;
|
|
|
|
AudioChunk& c = *ci;
|
|
|
|
uint32_t channels = c.mChannelData.Length();
|
|
|
|
output.SetLength(channels);
|
|
|
|
bufferPtrs.SetLength(channels);
|
|
|
|
uint32_t inFrames = c.mDuration,
|
|
|
|
outFrames = c.mDuration * aOutRate / aInRate;
|
|
|
|
for (uint32_t i = 0; i < channels; i++) {
|
|
|
|
const T* in = static_cast<const T*>(c.mChannelData[i]);
|
|
|
|
T* out = output[i].AppendElements(outFrames);
|
|
|
|
|
|
|
|
dom::WebAudioUtils::SpeexResamplerProcess(aResampler, i,
|
|
|
|
in, &inFrames,
|
|
|
|
out, &outFrames);
|
|
|
|
|
|
|
|
bufferPtrs[i] = out;
|
|
|
|
output[i].SetLength(outFrames);
|
|
|
|
}
|
|
|
|
c.mBuffer = new mozilla::SharedChannelArrayBuffer<T>(&output);
|
|
|
|
for (uint32_t i = 0; i < channels; i++) {
|
|
|
|
c.mChannelData[i] = bufferPtrs[i];
|
|
|
|
}
|
|
|
|
c.mDuration = outFrames;
|
|
|
|
mDuration += c.mDuration;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void ResampleChunks(SpeexResamplerState* aResampler);
|
|
|
|
|
2012-11-21 21:04:27 -08:00
|
|
|
void AppendFrames(already_AddRefed<ThreadSharedObject> aBuffer,
|
|
|
|
const nsTArray<const float*>& aChannelData,
|
|
|
|
int32_t aDuration)
|
2012-04-29 20:11:19 -07:00
|
|
|
{
|
2012-11-21 21:04:27 -08:00
|
|
|
AudioChunk* chunk = AppendChunk(aDuration);
|
2012-04-29 20:11:19 -07:00
|
|
|
chunk->mBuffer = aBuffer;
|
2012-11-21 21:04:27 -08: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 10:22:37 -08:00
|
|
|
#ifdef MOZILLA_INTERNAL_API
|
|
|
|
chunk->mTimeStamp = TimeStamp::Now();
|
|
|
|
#endif
|
2012-11-21 21:04:27 -08:00
|
|
|
}
|
|
|
|
void AppendFrames(already_AddRefed<ThreadSharedObject> aBuffer,
|
|
|
|
const nsTArray<const int16_t*>& aChannelData,
|
|
|
|
int32_t aDuration)
|
|
|
|
{
|
|
|
|
AudioChunk* chunk = AppendChunk(aDuration);
|
|
|
|
chunk->mBuffer = aBuffer;
|
|
|
|
for (uint32_t channel = 0; channel < aChannelData.Length(); ++channel) {
|
|
|
|
chunk->mChannelData.AppendElement(aChannelData[channel]);
|
|
|
|
}
|
2012-04-29 20:11:19 -07:00
|
|
|
chunk->mVolume = 1.0f;
|
2012-11-21 21:04:27 -08:00
|
|
|
chunk->mBufferFormat = AUDIO_FORMAT_S16;
|
2013-01-28 10:22:37 -08:00
|
|
|
#ifdef MOZILLA_INTERNAL_API
|
|
|
|
chunk->mTimeStamp = TimeStamp::Now();
|
|
|
|
#endif
|
2012-04-29 20:11:19 -07:00
|
|
|
}
|
2013-01-20 12:44:44 -08: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 10:22:37 -08:00
|
|
|
#ifdef MOZILLA_INTERNAL_API
|
|
|
|
chunk->mTimeStamp = TimeStamp::Now();
|
|
|
|
#endif
|
2013-01-20 12:44:44 -08:00
|
|
|
return chunk;
|
|
|
|
}
|
2012-04-29 20:11:19 -07:00
|
|
|
void ApplyVolume(float aVolume);
|
2014-03-24 03:06:06 -07:00
|
|
|
void WriteTo(uint64_t aID, AudioStream* aOutput, AudioMixer* aMixer = nullptr);
|
2014-03-24 03:06:05 -07:00
|
|
|
|
2014-03-24 03:06:05 -07:00
|
|
|
int ChannelCount() {
|
|
|
|
NS_WARN_IF_FALSE(!mChunks.IsEmpty(),
|
|
|
|
"Cannot query channel count on a AudioSegment with no chunks.");
|
|
|
|
return mChunks.IsEmpty() ? 0 : mChunks[0].mChannelData.Length();
|
|
|
|
}
|
|
|
|
|
2012-04-29 20:11:19 -07:00
|
|
|
static Type StaticType() { return AUDIO; }
|
|
|
|
};
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
#endif /* MOZILLA_AUDIOSEGMENT_H_ */
|