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
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
use std::path::Path;
use std::fs;
use std::io::Write;
extern crate hound;
pub struct OTSlice {
pub loop_point: u32,
pub start_point : u32,
pub length: u32
}
pub struct Slicer {
pub output_folder : String,
pub output_filename : String,
pub sample_rate : u32,
pub slices : Vec<OTSlice>,
start_offset : u32,
pub tempo : u32
}
impl Slicer {
pub fn new () -> Self {
Self {
slices : Vec::new(),
output_folder : "".to_string(),
output_filename : "output".to_string(),
sample_rate : 44100,
start_offset : 0,
tempo: 124
}
}
pub fn add_file (&mut self, filepath : String) -> Result< &'static str, &'static str> {
println!("Adding file to OT slice group: {}", filepath);
let spec = hound::WavSpec {
channels: 1,
sample_rate: self.sample_rate.clone(),
bits_per_sample: 16,
sample_format: hound::SampleFormat::Int,
};
let path : &Path = filepath.as_ref();
match path.is_file() {
true => {
let mut reader = hound::WavReader::open(filepath).unwrap();
if reader.spec() == spec {
let samples: Vec<i16> = reader.samples().map(|s| s.unwrap()).collect();
let output_folder_path : &Path = self.output_folder.as_ref();
let temp_file_path = Path::join(output_folder_path, "ot_tempfile.wav".to_string());
match temp_file_path.is_file() {
true => {
let mut temp_wav_file = hound::WavWriter::append(temp_file_path).unwrap();
for i in 0..samples.len() {
temp_wav_file.write_sample(samples[i].clone()).unwrap();
};
temp_wav_file.finalize().unwrap();
},
false => {
let mut temp_wav_file = hound::WavWriter::create(temp_file_path, spec).unwrap();
for i in 0..samples.len() {
temp_wav_file.write_sample(samples[i].clone()).unwrap();
};
temp_wav_file.finalize().unwrap();
}
}
let new_ot_slice = OTSlice{start_point: self.start_offset, length: samples.len() as u32, loop_point: 0xFFFFFFFF};
self.slices.push(new_ot_slice);
self.start_offset += samples.len() as u32;
Ok("File succesfully parsed.")
} else {
Err("Invalid file (invalid sample rate / bit rate / channel number)")
}
},
false => Err("File not found.")
}
}
pub fn generate_ot_file(&mut self) -> Result<&'static str, &'static str> {
let mut file_data : Vec<u8> = vec![0x46,0x4F,0x52,0x4D,0x00,0x00,0x00,0x00,0x44,0x50,0x53,0x31,0x53,0x4D,0x50,0x41, 0x00,0x00,0x00,0x00,0x00,0x02,0x00];
let tempo : u32 = self.tempo * 6 * 4;
let mut total_samples : u32 = 0;
for i in 0..self.slices.len() {
total_samples += self.slices[i].length;
}
println!("Generating Octatrack files...");
println!("Total samples: {}", total_samples);
let bars_mult : f32 = (124.0 *total_samples as f32) / (self.sample_rate * 60) as f32 + 0.5;
let bars : u32 = bars_mult as u32 * 25;
file_data = self.push_u32(file_data, tempo);
file_data = self.push_u32(file_data, bars.clone());
file_data = self.push_u32(file_data, bars.clone());
file_data = self.push_u32(file_data, 0);
file_data = self.push_u32(file_data, 0);
file_data = self.push_u16(file_data, 48);
file_data.push(255);
file_data = self.push_u32(file_data, 0);
file_data = self.push_u32(file_data, total_samples.clone());
file_data = self.push_u32(file_data, 0);
for i in 0..64 {
if i < self.slices.len() {
file_data = self.push_u32(file_data, self.slices[i].start_point);
file_data = self.push_u32(file_data, self.slices[i].start_point + self.slices[i].length);
file_data = self.push_u32(file_data, self.slices[i].loop_point);
} else {
file_data = self.push_u32(file_data, 0);
file_data = self.push_u32(file_data, 0);
file_data = self.push_u32(file_data, 0);
}
}
file_data = self.push_u32(file_data, self.slices.len() as u32);
println!("Number of slices: {}", self.slices.len());
let mut checksum : u16 = 0;
let len = file_data.len();
for i in 16..len {
checksum += file_data[i] as u16;
}
file_data = self.push_u16(file_data, checksum);
let output_folder_path : &Path = self.output_folder.as_ref();
let mut ot_file_name : String = self.output_filename.clone();
ot_file_name.push_str(".ot");
println!("\nGenerating Octatrack .ot file: {}", ot_file_name);
let ot_file_path = Path::join(output_folder_path, ot_file_name.clone());
if ot_file_path.is_file() {
fs::remove_file(ot_file_path.clone()).unwrap();
};
let mut buffer = fs::File::create(ot_file_path).unwrap();
buffer.write_all(&file_data).unwrap();
println!("Finished writing Octatrack .ot file.");
let output_folder_path : &Path = self.output_folder.as_ref();
let temp_file_path = Path::join(output_folder_path, "ot_tempfile.wav".to_string());
let mut wav_file_name : String = self.output_filename.clone();
wav_file_name.push_str(".wav");
let wav_file_path = Path::join(output_folder_path, wav_file_name);
let result = match fs::rename(temp_file_path, wav_file_path) {
Ok(_e) => {
Ok("Temporary WAV file renamed succesfully.")
},
Err(_e) => {
Err("Temporary WAV file not found.")
}
};
result
}
fn push_u32(&mut self, mut vector : Vec<u8>, num : u32) -> Vec<u8> {
let array = num.to_le_bytes();
for i in 0..4 {
vector.push(array[3-i]);
}
vector
}
fn push_u16(&mut self, mut vector : Vec<u8>, num : u16) -> Vec<u8> {
let array = num.to_le_bytes();
vector.push(array[1]);
vector.push(array[0]);
vector
}
}