Files
MuditaOS/module-audio/Audio/AudioFormat.cpp
Marcin Smoczyński 8b21df135c [EGD-5742] Add audio format checking
Check if decoder's file format is supported by the sink audio device
before playing the sound. Add list of supported formats to each of audio
devices.

Signed-off-by: Marcin Smoczyński <smoczynski.marcin@gmail.com>
2021-04-12 10:00:00 +02:00

90 lines
2.2 KiB
C++

// Copyright (c) 2017-2021, Mudita Sp. z.o.o. All rights reserved.
// For licensing, see https://github.com/mudita/MuditaOS/LICENSE.md
#include "AudioFormat.hpp"
using audio::AudioFormat;
auto AudioFormat::getSampleRate() const noexcept -> unsigned int
{
return sampleRate;
}
auto AudioFormat::getBitWidth() const noexcept -> unsigned int
{
return bitWidth;
}
auto AudioFormat::getChannels() const noexcept -> unsigned int
{
return channels;
}
auto AudioFormat::getBitrate() const noexcept -> unsigned long int
{
return sampleRate * bitWidth * channels;
}
auto AudioFormat::toString() const -> std::string
{
return "AudioFormat{" + std::to_string(sampleRate) + "," + std::to_string(bitWidth) + "," +
std::to_string(channels) + "}";
}
auto AudioFormat::operator==(const AudioFormat &other) const -> bool
{
return sampleRate == other.sampleRate && bitWidth == other.bitWidth && channels == other.channels;
}
auto AudioFormat::operator!=(const AudioFormat &other) const -> bool
{
return !operator==(other);
}
auto AudioFormat::operator>(const AudioFormat &other) const -> bool
{
return getBitrate() > other.getBitrate();
}
auto AudioFormat::operator<(const AudioFormat &other) const -> bool
{
return getBitrate() < other.getBitrate();
}
auto AudioFormat::operator<=(const AudioFormat &other) const -> bool
{
return getBitrate() <= other.getBitrate();
}
auto AudioFormat::operator>=(const AudioFormat &other) const -> bool
{
return getBitrate() >= other.getBitrate();
}
auto AudioFormat::isValid() const noexcept -> bool
{
return !(sampleRate == 0 || bitWidth == 0 || channels == 0);
}
auto AudioFormat::makeMatrix(std::set<unsigned int> sampleRates,
std::set<unsigned int> bitWidths,
std::set<unsigned int> channels) -> std::vector<AudioFormat>
{
std::vector<AudioFormat> v;
for (auto sampleRate : sampleRates) {
for (auto bitWidth : bitWidths) {
for (auto channelCount : channels) {
v.push_back(AudioFormat{sampleRate, bitWidth, channelCount});
}
}
}
return v;
}
auto AudioFormat::isNull() const noexcept -> bool
{
return operator==(audio::nullFormat);
}