Merge branch 'main' into devtools

This commit is contained in:
MihailRis
2024-05-17 15:47:25 +03:00
15 changed files with 120 additions and 107 deletions
+7 -4
View File
@@ -10,6 +10,7 @@
static debug::Logger logger("ogg");
namespace fs = std::filesystem;
using namespace audio;
static inline std::string vorbis_error_message(int code) {
@@ -33,7 +34,7 @@ static inline std::string vorbis_error_message(int code) {
}
}
audio::PCM* ogg::load_pcm(const std::filesystem::path& file, bool headerOnly) {
std::unique_ptr<audio::PCM> ogg::load_pcm(const fs::path& file, bool headerOnly) {
OggVorbis_File vf;
int code;
if ((code = ov_fopen(file.u8string().c_str(), &vf))) {
@@ -66,7 +67,9 @@ audio::PCM* ogg::load_pcm(const std::filesystem::path& file, bool headerOnly) {
totalSamples = data.size() / channels / 2;
}
ov_clear(&vf);
return new PCM(std::move(data), totalSamples, channels, 16, sampleRate, seekable);
return std::make_unique<PCM>(
std::move(data), totalSamples, channels, 16, sampleRate, seekable
);
}
class OggStream : public PCMStream {
@@ -149,11 +152,11 @@ public:
}
};
PCMStream* ogg::create_stream(const std::filesystem::path& file) {
std::unique_ptr<PCMStream> ogg::create_stream(const fs::path& file) {
OggVorbis_File vf;
int code;
if ((code = ov_fopen(file.u8string().c_str(), &vf))) {
throw std::runtime_error("vorbis: "+vorbis_error_message(code));
}
return new OggStream(std::move(vf));
return std::make_unique<OggStream>(std::move(vf));
}
+2 -2
View File
@@ -9,8 +9,8 @@ namespace audio {
}
namespace ogg {
extern audio::PCM* load_pcm(const std::filesystem::path& file, bool headerOnly);
extern audio::PCMStream* create_stream(const std::filesystem::path& file);
std::unique_ptr<audio::PCM> load_pcm(const std::filesystem::path& file, bool headerOnly);
std::unique_ptr<audio::PCMStream> create_stream(const std::filesystem::path& file);
}
#endif // CODERS_OGG_HPP_
+15 -8
View File
@@ -1,14 +1,18 @@
#include "wav.hpp"
#include "../audio/audio.hpp"
#include "../debug/Logger.hpp"
#include <vector>
#include <string>
#include <cstring>
#include <fstream>
#include <iostream>
#include <stdexcept>
namespace fs = std::filesystem;
static debug::Logger logger("wav-coder");
bool is_big_endian() {
uint32_t ui32_v = 0x01020304;
char bytes[sizeof(uint32_t)];
@@ -65,7 +69,7 @@ public:
return 0;
}
if (in.fail()) {
std::cerr << "Wav::load_pcm: I/O error ocurred" << std::endl;
logger.error() << "Wav::load_pcm: I/O error ocurred";
return -1;
}
return in.gcount();
@@ -114,7 +118,7 @@ public:
}
};
audio::PCMStream* wav::create_stream(const std::filesystem::path& file) {
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()+"'");
@@ -197,7 +201,6 @@ audio::PCMStream* wav::create_stream(const std::filesystem::path& file) {
}
if(std::strncmp(buffer, "data", 4) != 0){
std::cerr << buffer << std::endl;
throw std::runtime_error("file is not a valid WAVE file (doesn't have 'data' tag)");
}
@@ -214,11 +217,13 @@ audio::PCMStream* wav::create_stream(const std::filesystem::path& file) {
if(in.fail()){
throw std::runtime_error("fail state set on the file");
}
return new WavStream(std::move(in), channels, bitsPerSample, sampleRate, size, initialOffset);
return std::make_unique<WavStream>(
std::move(in), channels, bitsPerSample, sampleRate, size, initialOffset
);
}
audio::PCM* wav::load_pcm(const std::filesystem::path& file, bool headerOnly) {
std::unique_ptr<audio::PCMStream> stream(wav::create_stream(file));
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();
uint channels = stream->getChannels();
@@ -233,5 +238,7 @@ audio::PCM* wav::load_pcm(const std::filesystem::path& file, bool headerOnly) {
data.resize(size);
stream->readFully(data.data(), size, false);
}
return new audio::PCM(std::move(data), totalSamples, channels, bitsPerSample, sampleRate, true);
return std::make_unique<audio::PCM>(
std::move(data), totalSamples, channels, bitsPerSample, sampleRate, true
);
}
+2 -2
View File
@@ -9,8 +9,8 @@ namespace audio {
}
namespace wav {
extern audio::PCM* load_pcm(const std::filesystem::path& file, bool headerOnly);
extern audio::PCMStream* create_stream(const std::filesystem::path& file);
std::unique_ptr<audio::PCM> load_pcm(const std::filesystem::path& file, bool headerOnly);
std::unique_ptr<audio::PCMStream> create_stream(const std::filesystem::path& file);
}
#endif // CODERS_WAV_HPP_