// Copyright (c) 2017-2024, Mudita Sp. z.o.o. All rights reserved. // For licensing, see https://github.com/mudita/MuditaOS/blob/master/LICENSE.md #include "AudioFormat.hpp" #include 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 sampleRates, std::set bitWidths, std::set channels) -> std::vector { std::vector 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); } auto AudioFormat::bytesToMicroseconds(unsigned int bytes) const noexcept -> std::chrono::microseconds { auto bytesPerSecond = getBitrate() / utils::integer::BitsInByte; auto duration = std::chrono::duration(static_cast(bytes) / bytesPerSecond); return std::chrono::duration_cast(duration); } auto AudioFormat::microsecondsToBytes(std::chrono::microseconds duration) const noexcept -> unsigned int { auto bytesPerSecond = getBitrate() / utils::integer::BitsInByte; auto seconds = std::chrono::duration(duration).count(); return static_cast(seconds * bytesPerSecond); }