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"
|
|
|
|
#include "nsISupportsImpl.h"
|
|
|
|
#include "nsAudioStream.h"
|
|
|
|
#include "SharedBuffer.h"
|
|
|
|
|
|
|
|
namespace mozilla {
|
|
|
|
|
|
|
|
struct AudioChunk {
|
|
|
|
typedef nsAudioStream::SampleFormat SampleFormat;
|
|
|
|
|
|
|
|
// Generic methods
|
|
|
|
void SliceTo(TrackTicks aStart, TrackTicks aEnd)
|
|
|
|
{
|
|
|
|
NS_ASSERTION(aStart >= 0 && aStart < aEnd && aEnd <= mDuration,
|
|
|
|
"Slice out of bounds");
|
|
|
|
if (mBuffer) {
|
2012-08-22 08:56:38 -07:00
|
|
|
mOffset += 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) {
|
|
|
|
NS_ASSERTION(aOther.mBufferFormat == mBufferFormat && aOther.mBufferLength == mBufferLength,
|
|
|
|
"Wrong metadata about buffer");
|
|
|
|
return aOther.mOffset == mOffset + mDuration && aOther.mVolume == mVolume;
|
|
|
|
}
|
|
|
|
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-04-29 20:11:19 -07:00
|
|
|
mDuration = aDuration;
|
|
|
|
mOffset = 0;
|
|
|
|
mVolume = 1.0f;
|
|
|
|
}
|
|
|
|
|
|
|
|
TrackTicks mDuration; // in frames within the buffer
|
|
|
|
nsRefPtr<SharedBuffer> mBuffer; // null means data is all zeroes
|
2012-08-22 08:56:38 -07:00
|
|
|
int32_t mBufferLength; // number of frames in mBuffer (only meaningful if mBuffer is nonnull)
|
2012-04-29 20:11:19 -07:00
|
|
|
SampleFormat mBufferFormat; // format of frames in mBuffer (only meaningful if mBuffer is nonnull)
|
2012-08-22 08:56:38 -07:00
|
|
|
int32_t mOffset; // in frames within the buffer (zero if mBuffer is null)
|
2012-04-29 20:11:19 -07:00
|
|
|
float mVolume; // volume multiplier to apply (1.0f if mBuffer is nonnull)
|
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* 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:
|
|
|
|
typedef nsAudioStream::SampleFormat SampleFormat;
|
|
|
|
|
|
|
|
static int GetSampleSize(SampleFormat aFormat)
|
|
|
|
{
|
|
|
|
switch (aFormat) {
|
|
|
|
case nsAudioStream::FORMAT_U8: return 1;
|
2012-09-01 08:35:56 -07:00
|
|
|
case nsAudioStream::FORMAT_S16: return 2;
|
2012-04-29 20:11:19 -07:00
|
|
|
case nsAudioStream::FORMAT_FLOAT32: return 4;
|
|
|
|
}
|
|
|
|
NS_ERROR("Bad format");
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
AudioSegment() : MediaSegmentBase<AudioSegment, AudioChunk>(AUDIO), mChannels(0) {}
|
|
|
|
|
|
|
|
bool IsInitialized()
|
|
|
|
{
|
|
|
|
return mChannels > 0;
|
|
|
|
}
|
2012-08-22 08:56:38 -07:00
|
|
|
void Init(int32_t aChannels)
|
2012-04-29 20:11:19 -07:00
|
|
|
{
|
|
|
|
NS_ASSERTION(aChannels > 0, "Bad number of channels");
|
|
|
|
NS_ASSERTION(!IsInitialized(), "Already initialized");
|
|
|
|
mChannels = aChannels;
|
|
|
|
}
|
2012-08-22 08:56:38 -07:00
|
|
|
int32_t GetChannels()
|
2012-04-29 20:11:19 -07:00
|
|
|
{
|
|
|
|
NS_ASSERTION(IsInitialized(), "Not initialized");
|
|
|
|
return mChannels;
|
|
|
|
}
|
|
|
|
/**
|
|
|
|
* Returns the format of the first audio frame that has data, or
|
|
|
|
* FORMAT_FLOAT32 if there is none.
|
|
|
|
*/
|
|
|
|
SampleFormat GetFirstFrameFormat()
|
|
|
|
{
|
|
|
|
for (ChunkIterator ci(*this); !ci.IsEnded(); ci.Next()) {
|
|
|
|
if (ci->mBuffer) {
|
|
|
|
return ci->mBufferFormat;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return nsAudioStream::FORMAT_FLOAT32;
|
|
|
|
}
|
2012-08-22 08:56:38 -07:00
|
|
|
void AppendFrames(already_AddRefed<SharedBuffer> aBuffer, int32_t aBufferLength,
|
|
|
|
int32_t aStart, int32_t aEnd, SampleFormat aFormat)
|
2012-04-29 20:11:19 -07:00
|
|
|
{
|
|
|
|
NS_ASSERTION(mChannels > 0, "Not initialized");
|
|
|
|
AudioChunk* chunk = AppendChunk(aEnd - aStart);
|
|
|
|
chunk->mBuffer = aBuffer;
|
|
|
|
chunk->mBufferFormat = aFormat;
|
|
|
|
chunk->mBufferLength = aBufferLength;
|
|
|
|
chunk->mOffset = aStart;
|
|
|
|
chunk->mVolume = 1.0f;
|
|
|
|
}
|
|
|
|
void ApplyVolume(float aVolume);
|
|
|
|
/**
|
|
|
|
* aOutput must have a matching number of channels, but we will automatically
|
|
|
|
* convert sample formats.
|
|
|
|
*/
|
|
|
|
void WriteTo(nsAudioStream* aOutput);
|
|
|
|
|
|
|
|
// Segment-generic methods not in MediaSegmentBase
|
|
|
|
void InitFrom(const AudioSegment& aOther)
|
|
|
|
{
|
|
|
|
NS_ASSERTION(mChannels == 0, "Channels already set");
|
|
|
|
mChannels = aOther.mChannels;
|
|
|
|
}
|
2012-07-31 05:17:21 -07:00
|
|
|
void CheckCompatible(const AudioSegment& aOther) const
|
2012-04-29 20:11:19 -07:00
|
|
|
{
|
2012-07-31 05:17:21 -07:00
|
|
|
NS_ASSERTION(aOther.mChannels == mChannels, "Non-matching channels");
|
2012-04-29 20:11:19 -07:00
|
|
|
}
|
|
|
|
static Type StaticType() { return AUDIO; }
|
|
|
|
|
|
|
|
protected:
|
2012-08-22 08:56:38 -07:00
|
|
|
int32_t mChannels;
|
2012-04-29 20:11:19 -07:00
|
|
|
};
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
#endif /* MOZILLA_AUDIOSEGMENT_H_ */
|