diff options
author | lonkaars <loek@pipeframe.xyz> | 2023-06-03 16:00:17 +0200 |
---|---|---|
committer | lonkaars <loek@pipeframe.xyz> | 2023-06-03 16:00:17 +0200 |
commit | 31293dd9b427be45175a928cf55f90ea1bccebeb (patch) | |
tree | 6d5ae9f32347afa505b7d32607b9899f67b44f5b /os2eindopdracht | |
parent | 33fcc80be5e6d7c755a8bcc285854e872787ff38 (diff) |
multithreaded processing
Diffstat (limited to 'os2eindopdracht')
-rw-r--r-- | os2eindopdracht/.gitignore | 2 | ||||
-rw-r--r-- | os2eindopdracht/SampleStream.cpp | 3 | ||||
-rw-r--r-- | os2eindopdracht/SampleStream.h | 1 | ||||
-rw-r--r-- | os2eindopdracht/main.cpp | 16 | ||||
-rw-r--r-- | os2eindopdracht/makefile | 2 |
5 files changed, 22 insertions, 2 deletions
diff --git a/os2eindopdracht/.gitignore b/os2eindopdracht/.gitignore index 7c39037..181de15 100644 --- a/os2eindopdracht/.gitignore +++ b/os2eindopdracht/.gitignore @@ -5,3 +5,5 @@ main *.wav *.mp3 *.flac +.cache +compile_commands.json diff --git a/os2eindopdracht/SampleStream.cpp b/os2eindopdracht/SampleStream.cpp index 3727e52..e97c474 100644 --- a/os2eindopdracht/SampleStream.cpp +++ b/os2eindopdracht/SampleStream.cpp @@ -40,3 +40,6 @@ vector<SampleBlock> SampleStream::split(size_t block_size) { return out; } +void SampleStream::align(size_t block_size) { + return; +} diff --git a/os2eindopdracht/SampleStream.h b/os2eindopdracht/SampleStream.h index 4c8b5c3..5657b06 100644 --- a/os2eindopdracht/SampleStream.h +++ b/os2eindopdracht/SampleStream.h @@ -16,6 +16,7 @@ public: void load(string input); string save(); vector<SampleBlock> split(size_t block_size); + void align(size_t block_size); public: vector<int16_t> _stream; diff --git a/os2eindopdracht/main.cpp b/os2eindopdracht/main.cpp index 6d8ddd5..bc7ce17 100644 --- a/os2eindopdracht/main.cpp +++ b/os2eindopdracht/main.cpp @@ -3,6 +3,8 @@ #include <sstream> #include <fstream> #include <vector> +#include <semaphore> +#include <thread> #include "Arguments.h" #include "SampleStream.h" @@ -10,6 +12,8 @@ #include "FilterWorker.h" using std::vector; +using std::counting_semaphore; +using std::thread; #define SAMPLE_BLOCK_SIZE 1024 @@ -27,10 +31,20 @@ int main(int argc, char** argv) { SampleStream output(file_input_content.str()); vector<SampleBlock> output_blocks = output.split(SAMPLE_BLOCK_SIZE); + counting_semaphore semaphore(args.max_threads); + vector<thread> threads; for (size_t i = 0; i < input_blocks.size(); i++) { - FilterWorker(input_blocks[i], output_blocks[i]).filter(args.bass, args.treble); + semaphore.acquire(); + threads.push_back(thread([&args, &semaphore](SampleBlock& input, SampleBlock& output) { + FilterWorker(input, output).filter(args.bass, args.treble); + semaphore.release(); + }, std::ref(input_blocks[i]), std::ref(output_blocks[i]))); } + for (thread& t : threads) t.join(); + + output.align(SAMPLE_BLOCK_SIZE); + std::fstream file_output(args.file_output, std::ios::out | std::ios::binary); std::string edited_stream = output.save(); file_output.write(edited_stream.c_str(), edited_stream.size()); diff --git a/os2eindopdracht/makefile b/os2eindopdracht/makefile index 3d8f260..eb86336 100644 --- a/os2eindopdracht/makefile +++ b/os2eindopdracht/makefile @@ -1,7 +1,7 @@ C++ = g++ LD = g++ RM = rm -f -CFLAGS = +CFLAGS = -std=c++20 LFLAGS = TARGET = main |