Audio Sample Splitter
A program to read and split audio into instrument samples
 All Classes Functions
LiveRecordingSimulator.h
1 #include "httplib/httplib.h"
2 #include "json/json.h"
3 #include <iostream>
4 #include <ctime>
5 #include <map>
6 #include <string>
7 #include <chrono>
8 #include "elma.h"
9 #include "client.h"
10 #include <stdlib.h>
11 #include <stdexcept>
12 #include <math.h>
13 #include <stdio.h>
14 #include "AudioFile.h"
15 #include <fstream>
16 #include <vector>
17 #include "channel.h"
18 
19 using namespace std;
20 using namespace elma;
21 
24 class LiveRecordingSimulator : public Process {
25 
26  public:
27 
31  LiveRecordingSimulator(std::string filename, int buffer_size):Process("live recording simulator")
32  {
33  audioFile.load (filename);
34  bit_depth = audioFile.getBitDepth();
35  sample_rate = audioFile.getSampleRate();
36  bs = buffer_size;
37  split_audio_into_packets();
38  };
39 
40  void init() {}
42  void start() {
43  emit(Event("set bit depth", bit_depth));
44  emit(Event("set sample rate", sample_rate));
45  }
47  void update() {
48  if (i < left_data_packets.size()){
49  channel("left audio").send(left_data_packets[i]);
50  channel("right audio").send(right_data_packets[i]);
51  i++;
52  }
53  }
54  void stop() {}
55 
58  void split_audio_into_packets();
59 
60  private:
62  int i = 0;
63  AudioFile<double> audioFile;
64  double bit_depth;
65  double sample_rate;
66  vector<json> left_data_packets;
67  vector<json> right_data_packets;
69  int bs;
70 };
71 
73 
74  int buffer_size_counter = 0;
75  json left_buffer;
76  json right_buffer;
77 
78  for (int i = 0; i < audioFile.getNumSamplesPerChannel(); i++){
79  if(buffer_size_counter < bs){
80  left_buffer.push_back(audioFile.samples[0][i]);
81  right_buffer.push_back(audioFile.samples[1][i]);
82  buffer_size_counter++;
83  } else {
84  left_data_packets.push_back(left_buffer);
85  right_data_packets.push_back(right_buffer);
86  left_buffer.clear();
87  right_buffer.clear();
88  buffer_size_counter = 0;
89  }
90  }
91 
92  // Add the last data packet
93  left_data_packets.push_back(left_buffer);
94  right_data_packets.push_back(right_buffer);
95  left_buffer.clear();
96  right_buffer.clear();
97  buffer_size_counter = 0;
98 
99 }
LiveRecordingSimulator(std::string filename, int buffer_size)
void start()
Tells the manager its bit depth and sample rate when initialized.
void update()
Every update sends the next json audio data packet through the left and right channels.