-
Notifications
You must be signed in to change notification settings - Fork 14
/
Copy pathtest_drum_kit.cairo
74 lines (68 loc) · 2.22 KB
/
test_drum_kit.cairo
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
use crate::instruments::drum_kit::{get_drum_sound, DrumSound};
use crate::wave::WavFile;
use crate::tests::utils::to_hex;
#[test]
fn test_get_drum_kit() {
let sample_rate = 8000;
let bit_depth = 8;
// Get individual drum sounds
let kick = get_drum_sound(DrumSound::Kick, sample_rate, bit_depth).span();
let snare = get_drum_sound(DrumSound::Snare, sample_rate, bit_depth).span();
let hi_hat = get_drum_sound(DrumSound::HiHat, sample_rate, bit_depth).span();
let _bass = get_drum_sound(DrumSound::Bass, sample_rate, bit_depth).span();
let mut silence: Span<u32> = [0; 2000].span();
let mut samples: Array<u32> = array![];
// Demo pop rhythm
samples.append_span(kick);
samples.append_span(silence);
// Hi-hat
samples.append_span(hi_hat);
samples.append_span(silence);
// Snare
samples.append_span(snare);
samples.append_span(silence);
// Hi-hat
samples.append_span(hi_hat);
samples.append_span(silence);
// Kick
samples.append_span(kick);
samples.append_span(silence);
// Hi-hat
samples.append_span(hi_hat);
samples.append_span(silence);
// Snare
samples.append_span(snare);
samples.append_span(silence);
// Hi-hat
samples.append_span(hi_hat);
samples.append_span(silence);
// Kick
samples.append_span(kick);
samples.append_span(silence);
// Hi-hat
samples.append_span(hi_hat);
samples.append_span(silence);
// Snare
samples.append_span(snare);
samples.append_span(silence);
// Hi-hat
samples.append_span(hi_hat);
samples.append_span(silence);
let total_samples = samples.len();
// Create a WavFile struct
let wav = WavFile {
chunk_size: 36 + (total_samples * 8),
num_channels: 1, // Mono
sample_rate,
bits_per_sample: bit_depth,
subchunk2_size: total_samples * 8,
data: samples.span(),
};
// Convert WavFile to ByteArray
let res: ByteArray = wav.into();
assert!(res[0] == 'R', "First byte should be 'R'");
assert!(res[1] == 'I', "Second byte should be 'I'");
assert!(res[2] == 'F', "Third byte should be 'F'");
assert!(res[3] == 'F', "Fourth byte should be 'F'");
println!("{:}", to_hex(@res));
}