summaryrefslogtreecommitdiff
path: root/os2eindopdracht/SampleStream.cpp
blob: e037c5e3950e207eac445cbc8089225c31a17a24 (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
#include <algorithm>

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

using std::string;
using std::min;

SampleStream::SampleStream(string input_stream) {
  load(input_stream);
}

void SampleStream::load(string input) {
  _stream.clear();
  for (size_t 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(size_t block_size) {
	vector<SampleBlock> out;
	size_t stream_size = _stream.size();
	for (size_t i = 0; i < stream_size; i += block_size) {
		size_t begin_offset = i;
		size_t end_offset = min(i + block_size, stream_size);
		SampleBlock block(_stream.begin() + begin_offset, end_offset - begin_offset);
		out.push_back(block);
	}
	return out;
}