summaryrefslogtreecommitdiff
path: root/os2eindopdracht/SampleStream.cpp
blob: ca60281e1c707dfe89dd40b62cf7ea6222762f50 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
#include <algorithm>

#include "SampleStream.h"
#include "SampleBlock.h"

using std::string;
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) {
  _stream.clear();
  for (unsigned long i = 0; i < input.size(); i += 2)
    _stream.push_back(((input[i] & 0xff) << 0) | ((input[i+1] & 0xff) << 8));
}

string SampleStream::save() {
  string out;
  for (int16_t sample : _stream) {
    out.push_back((sample >> 0) & 0xff);
    out.push_back((sample >> 8) & 0xff);
  }
  return out;
}

vector<SampleBlock> SampleStream::split(unsigned long block_size) {
	vector<SampleBlock> out;
	unsigned long stream_size = _stream.size();
	for (unsigned long i = 0; i < stream_size; i += block_size) {
		unsigned long begin_offset = i;
		unsigned long end_offset = min(i + block_size, stream_size);
		SampleBlock block(_stream.begin() + begin_offset, _stream.begin() + end_offset);
		out.push_back(block);
	}
	return out;
}