format: reformat project

Signed-off-by: Vyacheslav Ivanov <islavaivanov76@gmail.com>
This commit is contained in:
Vyacheslav Ivanov
2024-08-03 19:53:48 +03:00
committed by Pugemon
parent 736cd175d5
commit bbf33e8e4d
202 changed files with 7389 additions and 5609 deletions
+62 -50
View File
@@ -1,13 +1,13 @@
#include "wav.hpp"
#include "../audio/audio.hpp"
#include "../debug/Logger.hpp"
#include <vector>
#include <string>
#include <cstring>
#include <fstream>
#include <stdexcept>
#include <string>
#include <vector>
#include "../audio/audio.hpp"
#include "../debug/Logger.hpp"
namespace fs = std::filesystem;
@@ -20,12 +20,11 @@ bool is_big_endian() {
return bytes[0] == 1;
}
std::int32_t convert_to_int(char* buffer, std::size_t len){
std::int32_t convert_to_int(char* buffer, std::size_t len) {
std::int32_t a = 0;
if (!is_big_endian()) {
std::memcpy(&a, buffer, len);
}
else {
} else {
for (std::size_t i = 0; i < len; ++i) {
reinterpret_cast<char*>(&a)[3 - i] = buffer[i];
}
@@ -44,18 +43,18 @@ class WavStream : public audio::PCMStream {
size_t initialPosition;
public:
WavStream(
std::ifstream in,
uint channels,
std::ifstream in,
uint channels,
uint bitsPerSample,
uint sampleRate,
size_t size,
size_t initialPosition
) : in(std::move(in)),
channels(channels),
bytesPerSample(bitsPerSample/8),
sampleRate(sampleRate),
totalSize(size)
{
)
: in(std::move(in)),
channels(channels),
bytesPerSample(bitsPerSample / 8),
sampleRate(sampleRate),
totalSize(size) {
totalSamples = totalSize / channels / bytesPerSample;
this->initialPosition = initialPosition;
}
@@ -74,10 +73,9 @@ public:
}
return in.gcount();
}
void close() override {
if (!isOpen())
return;
if (!isOpen()) return;
in.close();
}
@@ -110,58 +108,66 @@ public:
}
void seek(size_t position) override {
if (!isOpen())
return;
if (!isOpen()) return;
position %= totalSamples;
in.clear();
in.seekg(initialPosition + position * channels * bytesPerSample, std::ios_base::beg);
in.seekg(
initialPosition + position * channels * bytesPerSample,
std::ios_base::beg
);
}
};
std::unique_ptr<audio::PCMStream> wav::create_stream(const fs::path& file) {
std::ifstream in(file, std::ios::binary);
if(!in.is_open()){
throw std::runtime_error("could not to open file '"+file.u8string()+"'");
if (!in.is_open()) {
throw std::runtime_error(
"could not to open file '" + file.u8string() + "'"
);
}
char buffer[6];
// the RIFF
if(!in.read(buffer, 4)){
if (!in.read(buffer, 4)) {
throw std::runtime_error("could not to read RIFF");
}
if(std::strncmp(buffer, "RIFF", 4) != 0){
throw std::runtime_error("file is not a valid WAVE file (header doesn't begin with RIFF)");
if (std::strncmp(buffer, "RIFF", 4) != 0) {
throw std::runtime_error(
"file is not a valid WAVE file (header doesn't begin with RIFF)"
);
}
// the size of the file
if(!in.read(buffer, 4)){
if (!in.read(buffer, 4)) {
throw std::runtime_error("could not read size of file");
}
// the WAVE
if(!in.read(buffer, 4)){
if (!in.read(buffer, 4)) {
throw std::runtime_error("could not to read WAVE");
}
if(std::strncmp(buffer, "WAVE", 4) != 0){
throw std::runtime_error("file is not a valid WAVE file (header doesn't contain WAVE)");
if (std::strncmp(buffer, "WAVE", 4) != 0) {
throw std::runtime_error(
"file is not a valid WAVE file (header doesn't contain WAVE)"
);
}
// "fmt/0"
if(!in.read(buffer, 4)){
if (!in.read(buffer, 4)) {
throw std::runtime_error("could not read fmt/0");
}
// this is always 16, the size of the fmt data chunk
if(!in.read(buffer, 4)){
if (!in.read(buffer, 4)) {
throw std::runtime_error("could not read the 16");
}
// PCM should be 1?
if(!in.read(buffer, 2)){
if (!in.read(buffer, 2)) {
throw std::runtime_error("could not read PCM");
}
// the number of channels
if(!in.read(buffer, 2)){
if (!in.read(buffer, 2)) {
throw std::runtime_error("could not read number of channels");
}
int channels = convert_to_int(buffer, 2);
// sample rate
if(!in.read(buffer, 4)){
if (!in.read(buffer, 4)) {
throw std::runtime_error("could not read sample rate");
}
int sampleRate = convert_to_int(buffer, 4);
@@ -170,16 +176,19 @@ std::unique_ptr<audio::PCMStream> wav::create_stream(const fs::path& file) {
}
// bitsPerSample
if(!in.read(buffer, 2)){
if (!in.read(buffer, 2)) {
throw std::runtime_error("could not read bits per sample");
}
int bitsPerSample = convert_to_int(buffer, 2);
if (bitsPerSample >= 24) {
throw std::runtime_error(std::to_string(bitsPerSample)+" bit depth is not supported by OpenAL");
throw std::runtime_error(
std::to_string(bitsPerSample) +
" bit depth is not supported by OpenAL"
);
}
// data chunk header "data"
if(!in.read(buffer, 4)){
if (!in.read(buffer, 4)) {
throw std::runtime_error("could not read data chunk header");
}
@@ -187,7 +196,7 @@ std::unique_ptr<audio::PCMStream> wav::create_stream(const fs::path& file) {
// skip garbage in WAV
if (std::strncmp(buffer, "LIST", 4) == 0) {
// chunk size
if(!in.read(buffer, 4)){
if (!in.read(buffer, 4)) {
throw std::runtime_error("could not read comment chunk size");
}
int chunkSize = convert_to_int(buffer, 4);
@@ -195,26 +204,28 @@ std::unique_ptr<audio::PCMStream> wav::create_stream(const fs::path& file) {
initialOffset += chunkSize + 4;
if(!in.read(buffer, 4)){
if (!in.read(buffer, 4)) {
throw std::runtime_error("could not read data chunk header");
}
}
if(std::strncmp(buffer, "data", 4) != 0){
throw std::runtime_error("file is not a valid WAVE file (doesn't have 'data' tag)");
if (std::strncmp(buffer, "data", 4) != 0) {
throw std::runtime_error(
"file is not a valid WAVE file (doesn't have 'data' tag)"
);
}
// size of data
if(!in.read(buffer, 4)){
if (!in.read(buffer, 4)) {
throw std::runtime_error("could not read data size");
}
size_t size = convert_to_int(buffer, 4);
/* cannot be at the end of file */
if(in.eof()){
if (in.eof()) {
throw std::runtime_error("reached EOF on the file");
}
if(in.fail()){
if (in.fail()) {
throw std::runtime_error("fail state set on the file");
}
return std::make_unique<WavStream>(
@@ -222,7 +233,9 @@ std::unique_ptr<audio::PCMStream> wav::create_stream(const fs::path& file) {
);
}
std::unique_ptr<audio::PCM> wav::load_pcm(const fs::path& file, bool headerOnly) {
std::unique_ptr<audio::PCM> wav::load_pcm(
const fs::path& file, bool headerOnly
) {
auto stream = wav::create_stream(file);
size_t totalSamples = stream->getTotalSamples();
@@ -232,9 +245,8 @@ std::unique_ptr<audio::PCM> wav::load_pcm(const fs::path& file, bool headerOnly)
std::vector<char> data;
if (!headerOnly) {
size_t size = stream->getTotalSamples() *
(stream->getBitsPerSample()/8) *
stream->getChannels();
size_t size = stream->getTotalSamples() *
(stream->getBitsPerSample() / 8) * stream->getChannels();
data.resize(size);
stream->readFully(data.data(), size, false);
}