Torch
Loading...
Searching...
No Matches
AudioManager.h
1#pragma once
2
3#include <map>
4#include <vector>
5#include <string>
6#include <iostream>
7#include <yaml-cpp/yaml.h>
8#include "lib/binarytools/BinaryWriter.h"
9#include <variant>
10#include <optional>
11
12#define NONE 0xFFFF
13#define ALIGN(val, al) (size_t) ((val + (al - 1)) & -al)
14
15struct Entry {
16 uint32_t offset;
17 uint32_t length;
18};
19
21 uint32_t offset;
22 float tuning;
23 bool operator==(const AudioBankSound& other) const {
24 return offset == other.offset && tuning == other.tuning;
25 }
26};
27
28struct Instrument {
29 bool valid;
30 std::string name;
31 uint32_t offset;
32 uint8_t releaseRate;
33 uint8_t normalRangeLo;
34 uint8_t normalRangeHi;
35 uint32_t envelope;
36 std::optional<AudioBankSound> soundLo;
37 std::optional<AudioBankSound> soundMed;
38 std::optional<AudioBankSound> soundHi;
39};
40
41struct AdpcmBook {
42 int32_t order{};
43 int32_t npredictors{};
44 std::vector<int16_t> table;
45 bool operator==(const AdpcmBook& other) const {
46 return order == other.order && npredictors == other.npredictors && table == other.table;
47 }
48};
49
50struct Drum {
51 std::string name;
52 uint32_t offset;
53 uint8_t releaseRate;
54 uint8_t pan;
55 uint32_t envelope;
56 AudioBankSound sound;
57 bool operator==(const Drum& other) const {
58 return name == other.name && offset == other.offset && releaseRate == other.releaseRate && pan == other.pan && envelope == other.envelope && sound == other.sound;
59 }
60};
61
62struct AdpcmLoop {
63 uint32_t start{};
64 uint32_t end{};
65 int32_t count{};
66 uint32_t pad{};
67 std::optional<std::vector<int16_t>> state;
68 bool operator==(const AdpcmLoop& other) const {
69 return start == other.start && end == other.end && count == other.count && pad == other.pad && state == other.state;
70 }
71};
72
74 std::string name;
75 std::vector<uint8_t> data;
76 AdpcmBook book;
77 AdpcmLoop loop;
78 std::vector<float> tunings;
79};
80
81struct SampleBank {
82 std::string name;
83 uint32_t offset;
84 std::vector<uint8_t> data;
85 std::map<uint32_t, AudioBankSample*> entries;
86
87 AudioBankSample* AddSample(uint32_t addr, size_t sampleSize, const AdpcmBook& book, const AdpcmLoop& loop);
88};
89
90struct TBLFile {
91 std::vector<SampleBank*> banks;
92 std::vector<std::string> tbls;
93 std::map<std::string, SampleBank*> map;
94};
95
96struct CTLHeader {
97 uint32_t instruments;
98 uint32_t numDrums;
99 uint32_t shared;
100};
101
103 int16_t delay;
104 int16_t arg;
105};
106
107struct Envelope {
108 std::string name;
109 std::vector<AdsrEnvelope> entries;
110};
111
112struct Bank {
113 std::string name;
114 SampleBank* sampleBank;
115 std::vector<Instrument> insts;
116 std::vector<Drum> drums;
117 std::vector<std::variant<Instrument, std::vector<Drum>>> allInsts;
118 std::vector<uint32_t> instOffsets;
119 std::map<uint32_t, Envelope> envelopes;
120 std::map<uint32_t, AudioBankSample*> samples;
121 void print() const;
122};
123
125public:
126 static AudioManager* Instance;
127 void initialize(std::vector<uint8_t>& buffer, YAML::Node& data);
128
129 // Optional AUDIO_HEADER yaml settings: `dialect` (US/EU opcode set) and
130 // `frequency` (session rate the bank tunings are relative to).
131 const std::string& GetDialect() const { return dialect; }
132 uint32_t GetSessionFrequency() const { return sessionFrequency; }
133 std::string dialect;
134 uint32_t sessionFrequency = 0;
135 void bind_sample(YAML::Node& node, const std::string& path);
136 std::string& get_sample(uint32_t id);
137 AudioBankSample get_aifc(int32_t index);
138 std::map<uint32_t, Bank> get_banks();
139 std::vector<SampleBank*> get_loaded_banks();
140 std::vector<AudioBankSample*> get_samples();
141 uint32_t get_index(AudioBankSample* bank);
142private:
143 std::map<uint32_t, Bank> banks;
144 std::map<AudioBankSample*, uint32_t> sampleMap;
145 TBLFile loaded_tbl;
146
147 static std::vector<Entry> parse_seq_file(std::vector<uint8_t>& buffer, uint32_t offset, bool isCTL);
148 static CTLHeader parse_ctl_header(std::vector<uint8_t>& data);
149 static std::optional<AudioBankSound> parse_sound(std::vector<uint8_t> data);
150 static Drum parse_drum(std::vector<uint8_t>& data, uint32_t addr);
151 static Instrument parse_inst(std::vector<uint8_t>& data, uint32_t addr);
152 static AdpcmLoop parse_loop(uint32_t addr, std::vector<uint8_t>& bankData);
153 static AdpcmBook parse_book(uint32_t addr, std::vector<uint8_t>& bankData);
154 static AudioBankSample* parse_sample(std::vector<uint8_t>& data, std::vector<uint8_t>& bankData, SampleBank* sampleBank);
155 static std::vector<AdsrEnvelope> parse_envelope(uint32_t addr, std::vector<uint8_t>& dataBank);
156 static Bank parse_ctl(CTLHeader header, std::vector<uint8_t> data, SampleBank* bank, uint32_t index);
157 static TBLFile parse_tbl(std::vector<uint8_t>& data, std::vector<Entry>& entries);
158};
Definition AudioManager.h:124
Definition AudioManager.h:41
Definition AudioManager.h:62
Definition AudioManager.h:102
Definition AudioManager.h:73
Definition AudioManager.h:20
Definition AudioManager.h:112
Definition AudioManager.h:96
Definition AudioManager.h:50
Definition AudioManager.h:15
Definition AudioManager.h:107
Definition AudioManager.h:28
Definition AudioManager.h:81
Definition AudioManager.h:90