From 4ddbd609e537acbf5610651772d969ea59f99a4c Mon Sep 17 00:00:00 2001 From: lonkaars Date: Sat, 3 Jun 2023 16:21:05 +0200 Subject: add comments and remove non-working align method --- os2eindopdracht/main.cpp | 19 +++++++++++++++---- 1 file changed, 15 insertions(+), 4 deletions(-) (limited to 'os2eindopdracht/main.cpp') diff --git a/os2eindopdracht/main.cpp b/os2eindopdracht/main.cpp index bc7ce17..b451652 100644 --- a/os2eindopdracht/main.cpp +++ b/os2eindopdracht/main.cpp @@ -14,37 +14,48 @@ using std::vector; using std::counting_semaphore; using std::thread; +using std::ref; #define SAMPLE_BLOCK_SIZE 1024 int main(int argc, char** argv) { + // parse arguments Arguments args(argc, argv); + // read input file to std::string std::fstream file_input(args.file_input, std::ios::in | std::ios::binary); std::stringstream file_input_content; file_input_content << file_input.rdbuf(); file_input.close(); + // convert std::string to vector SampleStream input(file_input_content.str()); + // split into 1024-sample blocks vector input_blocks = input.split(SAMPLE_BLOCK_SIZE); + // do the same for output stream (input stream is later used as reference but not mutated) SampleStream output(file_input_content.str()); vector output_blocks = output.split(SAMPLE_BLOCK_SIZE); + // c++20 semaphore for limiting active thread count counting_semaphore semaphore(args.max_threads); + // thread array vector threads; for (size_t i = 0; i < input_blocks.size(); i++) { - semaphore.acquire(); + semaphore.acquire(); // wait until less than args.max_threads threads are running + // create thread threads.push_back(thread([&args, &semaphore](SampleBlock& input, SampleBlock& output) { + // apply filter to output FilterWorker(input, output).filter(args.bass, args.treble); + // allow new thread to be created semaphore.release(); - }, std::ref(input_blocks[i]), std::ref(output_blocks[i]))); + }, ref(input_blocks[i]), ref(output_blocks[i]))); } + // make sure all threads are done for (thread& t : threads) t.join(); - output.align(SAMPLE_BLOCK_SIZE); - + // write pcm stream to output file 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()); -- cgit v1.2.3