summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorlonkaars <loek@pipeframe.xyz>2023-06-03 16:21:05 +0200
committerlonkaars <loek@pipeframe.xyz>2023-06-03 16:21:19 +0200
commit4ddbd609e537acbf5610651772d969ea59f99a4c (patch)
treec5add84a52e6eba996896152b2c4d3fabf62779f
parent31293dd9b427be45175a928cf55f90ea1bccebeb (diff)
add comments and remove non-working align method
-rw-r--r--os2eindopdracht/Arguments.h3
-rw-r--r--os2eindopdracht/FilterWorker.h2
-rw-r--r--os2eindopdracht/SampleStream.cpp6
-rw-r--r--os2eindopdracht/SampleStream.h4
-rw-r--r--os2eindopdracht/main.cpp19
5 files changed, 23 insertions, 11 deletions
diff --git a/os2eindopdracht/Arguments.h b/os2eindopdracht/Arguments.h
index c61d3a9..c5c9cd5 100644
--- a/os2eindopdracht/Arguments.h
+++ b/os2eindopdracht/Arguments.h
@@ -4,6 +4,7 @@
using std::string;
+/** @brief wrapper for magic numbers */
struct Coefficients {
double b0;
double b1;
@@ -14,6 +15,7 @@ struct Coefficients {
class Arguments {
public:
+ /** @brief parse arguments using getopt */
Arguments(int argc, char** argv);
public:
@@ -24,6 +26,7 @@ public:
Coefficients treble;
private:
+ /** @brief calculate magic coefficients using bass and treble gain value */
void calculate_coefficients();
private:
diff --git a/os2eindopdracht/FilterWorker.h b/os2eindopdracht/FilterWorker.h
index 2883d8b..f5fdb6d 100644
--- a/os2eindopdracht/FilterWorker.h
+++ b/os2eindopdracht/FilterWorker.h
@@ -5,7 +5,9 @@
class FilterWorker {
public:
+ /** @brief set input and output sample block */
FilterWorker(SampleBlock& input, SampleBlock& output);
+ /** @brief apply filtering using bass and treble coefficients to output sample block */
void filter(Coefficients bass, Coefficients treble);
private:
diff --git a/os2eindopdracht/SampleStream.cpp b/os2eindopdracht/SampleStream.cpp
index e97c474..e037c5e 100644
--- a/os2eindopdracht/SampleStream.cpp
+++ b/os2eindopdracht/SampleStream.cpp
@@ -8,9 +8,6 @@ using std::min;
SampleStream::SampleStream(string input_stream) {
load(input_stream);
-
- // test direct sample editing:
- // std::transform(_stream.begin(), _stream.end(), _stream.begin(), [](int16_t s) { return s/2; });
}
void SampleStream::load(string input) {
@@ -40,6 +37,3 @@ 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 5657b06..7ff5594 100644
--- a/os2eindopdracht/SampleStream.h
+++ b/os2eindopdracht/SampleStream.h
@@ -13,10 +13,12 @@ public:
SampleStream(string input_stream);
public:
+ /** @brief load raw pcm sample bytes (signed 16-bit little endian) to _stream */
void load(string input);
+ /** @brief print raw pcb sample bytes from _stream */
string save();
+ /** @brief return array of SampleBlock for _stream using block_size */
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 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<int16_t>
SampleStream input(file_input_content.str());
+ // split into 1024-sample blocks
vector<SampleBlock> 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<SampleBlock> output_blocks = output.split(SAMPLE_BLOCK_SIZE);
+ // c++20 semaphore for limiting active thread count
counting_semaphore semaphore(args.max_threads);
+ // thread array
vector<thread> 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());