Skip to content

Commit b4fa1ac

Browse files
silent-ciphertekkac
authored andcommitted
adjusted wave amplitude limits
1 parent 5fa044c commit b4fa1ac

File tree

3 files changed

+39
-22
lines changed

3 files changed

+39
-22
lines changed

src/contract/notes.cairo

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ mod NotesContract {
2020
#[abi(embed_v0)]
2121
impl NotesContractImpl of INotesContract<ContractState> {
2222
fn get_note(self: @ContractState, note: Note) -> ByteArray {
23-
let music = Music { notes: array![note].span(), sample_rate: 8000, bit_depth: 32 };
23+
let music = Music { notes: array![note].span(), sample_rate: 8000, bit_depth: 16 };
2424
let wav: WavFile = music.into();
2525
wav.into()
2626
}
@@ -42,7 +42,7 @@ mod NotesContract {
4242
]
4343
.span(),
4444
sample_rate: 8000,
45-
bit_depth: 32,
45+
bit_depth: 16,
4646
};
4747
let wav: WavFile = music.into();
4848
wav.into()

src/utils.cairo

Lines changed: 20 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -5,11 +5,15 @@ const PRECISION: u64 = 1_000_000;
55

66
fn get_max_value(bit_depth: u16) -> u64 {
77
if bit_depth != 4 && bit_depth != 8 && bit_depth != 16 && bit_depth != 24 && bit_depth != 32 {
8-
panic!("Unsupported bit depth");
8+
panic!("Unsupported bit depth");
9+
} else if bit_depth == 4 {
10+
return 15;
11+
} else if bit_depth == 8 {
12+
return 255;
913
}
1014
let mut n = bit_depth;
1115
let mut result: u64 = 1;
12-
while n > 0 {
16+
while n > 1 {
1317
result *= 2;
1418
n -= 1;
1519
};
@@ -18,20 +22,23 @@ fn get_max_value(bit_depth: u16) -> u64 {
1822
// TODO: Test append to array version for efficiency
1923

2024
// TODO
21-
fn generate_sine_wave(frequency_hz: u32, duration_ms: u32, sample_rate_hz: u32, bit_depth: u16) -> Array<u32> {
25+
fn generate_sine_wave(
26+
frequency_hz: u32, duration_ms: u32, sample_rate_hz: u32, bit_depth: u16
27+
) -> Array<u32> {
2228
let samples = array![];
2329
samples
2430
}
2531

26-
fn generate_square_wave(frequency_hz: u32, duration_ms: u32, sample_rate_hz: u32, bit_depth: u16) -> Array<u32> {
32+
fn generate_square_wave(
33+
frequency_hz: u32, duration_ms: u32, sample_rate_hz: u32, bit_depth: u16
34+
) -> Array<u32> {
2735
let mut samples: Array<u32> = array![];
2836
let mut num_samples_left: u64 = ((duration_ms * sample_rate_hz) / 1000).into();
2937

3038
let mega_full: u64 = (PRECISION * sample_rate_hz.into()) / frequency_hz.into();
3139
let mega_half: u64 = mega_full / 2;
3240

3341
let max_value: u64 = get_max_value(bit_depth);
34-
3542
while num_samples_left > 0 {
3643
if (num_samples_left * PRECISION) % mega_full < mega_half {
3744
samples.append(0);
@@ -44,13 +51,14 @@ fn generate_square_wave(frequency_hz: u32, duration_ms: u32, sample_rate_hz: u32
4451
}
4552

4653
// TODO
47-
fn generate_sawtooth_wave(frequency_hz: u32, duration_ms: u32, sample_rate_hz: u32, bit_depth: u16) -> Array<u32> {
54+
fn generate_sawtooth_wave(
55+
frequency_hz: u32, duration_ms: u32, sample_rate_hz: u32, bit_depth: u16
56+
) -> Array<u32> {
4857
let mut samples: Array<u32> = array![];
4958
let mut num_samples_left: u64 = ((duration_ms * sample_rate_hz) / 1000).into();
5059

5160
let mega_full: u64 = (PRECISION * sample_rate_hz.into()) / frequency_hz.into();
5261

53-
5462
let max_value: u64 = get_max_value(bit_depth);
5563

5664
while num_samples_left > 0 {
@@ -63,15 +71,16 @@ fn generate_sawtooth_wave(frequency_hz: u32, duration_ms: u32, sample_rate_hz: u
6371
samples
6472
}
6573

66-
fn generate_triangle_wave(frequency_hz: u32, duration_ms: u32, sample_rate_hz: u32, bit_depth: u16) -> Array<u32> {
74+
fn generate_triangle_wave(
75+
frequency_hz: u32, duration_ms: u32, sample_rate_hz: u32, bit_depth: u16
76+
) -> Array<u32> {
6777
let mut samples: Array<u32> = array![];
6878
let mut num_samples_left: u64 = ((duration_ms * sample_rate_hz) / 1000).into();
6979

7080
let mega_full: u64 = (PRECISION * sample_rate_hz.into()) / frequency_hz.into();
7181
let mega_half: u64 = mega_full / 2;
7282

7383
let max_value: u64 = get_max_value(bit_depth);
74-
7584
while num_samples_left > 0 {
7685
let pos = (num_samples_left * PRECISION) % mega_full;
7786
let value = if pos < mega_half {
@@ -83,4 +92,5 @@ fn generate_triangle_wave(frequency_hz: u32, duration_ms: u32, sample_rate_hz: u
8392
num_samples_left -= 1;
8493
};
8594
samples
86-
}
95+
}
96+

src/wave.cairo

Lines changed: 17 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,6 @@ struct WavFile {
1919
}
2020

2121

22-
2322
impl WavToBytes of Into<WavFile, ByteArray> {
2423
fn into(self: WavFile) -> ByteArray {
2524
let mut bytes: ByteArray = "";
@@ -47,17 +46,25 @@ impl WavToBytes of Into<WavFile, ByteArray> {
4746
// Append data
4847
let mut count = 0;
4948
if self.bits_per_sample == 4_u16 {
50-
while self.data.len() - count > 1 {
51-
bytes.append_byte((*self.data[count]| *self.data[count + 1]).try_into().unwrap());
52-
count += 2;
53-
};
49+
while self.data.len()
50+
- count > 1 {
51+
bytes
52+
.append_byte(
53+
(*self.data[count] | *self.data[count + 1]).try_into().unwrap()
54+
);
55+
count += 2;
56+
};
5457
} else {
55-
while self.data.len() - count > 0 {
56-
bytes.append_word_rev((*self.data[count]).into(), self.bits_per_sample.into() / 8_u32);
57-
count += 1;
58-
};
58+
while self.data.len()
59+
- count > 0 {
60+
bytes
61+
.append_word_rev(
62+
(*self.data[count]).into(), self.bits_per_sample.into() / 8_u32
63+
);
64+
count += 1;
65+
};
5966
}
60-
67+
6168
bytes
6269
}
6370
}

0 commit comments

Comments
 (0)