// Copyright (c) 2012-2023 Wojciech Figat. All rights reserved.
#pragma once
#include "Engine/Audio/Types.h"
#include "Engine/Core/Collections/Array.h"
class ReadStream;
///
/// Interface used for implementations that parse audio formats into a set of PCM samples.
///
class AudioDecoder
{
public:
///
/// Finalizes an instance of the class.
///
virtual ~AudioDecoder()
{
}
public:
///
/// Tries to open the specified stream with audio data and loads the whole audio data.
///
/// The data stream audio data is stored in. Must be valid until decoder usage end. Decoder may cache this pointer for the later usage.
/// The output information describing meta-data of the audio in the stream.
/// The output data.
/// The offset.
/// True if the data is invalid or conversion failed, otherwise false.
virtual bool Convert(ReadStream* stream, AudioDataInfo& info, Array& result, uint32 offset = 0)
{
if (!IsValid(stream, offset))
return true;
if (!Open(stream, info, offset))
return true;
// Load the whole audio data
const int32 bytesPerSample = info.BitDepth / 8;
const int32 bufferSize = info.NumSamples * bytesPerSample;
result.Resize(bufferSize);
Read(result.Get(), info.NumSamples);
return false;
}
public:
///
/// Tries to open the specified stream with audio data. Must be called before any reads or seeks.
///
/// The data stream audio data is stored in. Must be valid until decoder usage end. Decoder may cache this pointer for the later usage.
/// The output information describing meta-data of the audio in the stream.
/// The offset.
/// True if the data is invalid, otherwise false.
virtual bool Open(ReadStream* stream, AudioDataInfo& info, uint32 offset = 0) = 0;
///
/// Moves the read pointer to the specified offset. Any further Read() calls will read from this location. User must ensure not to seek past the end of the data.
///
/// The offset to move the pointer in. In number of samples.
virtual void Seek(uint32 offset) = 0;
///
/// Reads a set of samples from the audio data.
///
///
/// All values are returned as signed values.
///
/// Pre-allocated buffer to store the samples in.
/// The number of samples to read.
virtual void Read(byte* samples, uint32 numSamples) = 0;
///
/// Checks if the data in the provided stream valid audio data for the current format. You should check this before calling Open().
///
/// The stream to check.
/// The offset at which audio data in the stream begins, in bytes.
/// True if the data is valid, otherwise false.
virtual bool IsValid(ReadStream* stream, uint32 offset = 0) = 0;
};