Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: implemented sine wave #18

Closed
wants to merge 5 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Binary file added .DS_Store
Binary file not shown.
65 changes: 65 additions & 0 deletions Scarb.lock
Original file line number Diff line number Diff line change
@@ -1,6 +1,71 @@
# Code generated by scarb DO NOT EDIT.
version = 1

[[package]]
name = "alexandria_data_structures"
version = "0.2.0"
source = "git+https://github.com/keep-starknet-strange/alexandria.git?rev=800f5ad#800f5ad217847b5ded63c0302a444161766ee9d6"
dependencies = [
"alexandria_encoding",
]

[[package]]
name = "alexandria_encoding"
version = "0.1.0"
source = "git+https://github.com/keep-starknet-strange/alexandria.git?rev=800f5ad#800f5ad217847b5ded63c0302a444161766ee9d6"
dependencies = [
"alexandria_math",
"alexandria_numeric",
]

[[package]]
name = "alexandria_math"
version = "0.2.0"
source = "git+https://github.com/keep-starknet-strange/alexandria.git?rev=800f5ad#800f5ad217847b5ded63c0302a444161766ee9d6"
dependencies = [
"alexandria_data_structures",
]

[[package]]
name = "alexandria_merkle_tree"
version = "0.1.0"
source = "git+https://github.com/keep-starknet-strange/alexandria.git?rev=800f5ad#800f5ad217847b5ded63c0302a444161766ee9d6"

[[package]]
name = "alexandria_numeric"
version = "0.1.0"
source = "git+https://github.com/keep-starknet-strange/alexandria.git?rev=800f5ad#800f5ad217847b5ded63c0302a444161766ee9d6"
dependencies = [
"alexandria_math",
]

[[package]]
name = "alexandria_sorting"
version = "0.1.0"
source = "git+https://github.com/keep-starknet-strange/alexandria.git?rev=800f5ad#800f5ad217847b5ded63c0302a444161766ee9d6"
dependencies = [
"alexandria_data_structures",
]

[[package]]
name = "cairo_wave"
version = "0.1.0"
dependencies = [
"orion",
]

[[package]]
name = "cubit"
version = "1.3.0"
source = "git+https://github.com/influenceth/cubit.git?rev=6275608#62756082bf2555d7ab25c69d9c7bc30574ff1ce8"

[[package]]
name = "orion"
version = "0.2.5"
source = "git+https://github.com/gizatechxyz/onnx-cairo#efbe4aad518eeb9ccea778978da0323e5865d2cb"
dependencies = [
"alexandria_data_structures",
"alexandria_merkle_tree",
"alexandria_sorting",
"cubit",
]
1 change: 1 addition & 0 deletions Scarb.toml
Original file line number Diff line number Diff line change
Expand Up @@ -9,3 +9,4 @@ sierra = true

[dependencies]
starknet = ">=2.6.0"
orion = { git = "https://github.com/gizatechxyz/onnx-cairo" }
Binary file added src/.DS_Store
Binary file not shown.
14 changes: 14 additions & 0 deletions src/contract/notes.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,20 @@ mod tests {
println!("{:}", res);
}

#[test]
fn test_sine_wave() {
let contract = deploy();
let note = Note { frequency_hz: 440, duration_ms: 1500, note_type: NoteType::Sine };

let res: ByteArray = contract.get_note(note);
assert!(res[0] == 'R');
assert!(res[1] == 'I');
assert!(res[2] == 'F');
assert!(res[3] == 'F');
println!("{:}", res);
}


#[test]
fn test_get_notes() {
let contract = deploy();
Expand Down
27 changes: 26 additions & 1 deletion src/utils.cairo
Original file line number Diff line number Diff line change
@@ -1,12 +1,37 @@
use core::option::OptionTrait;
use core::traits::TryInto;
use orion::numbers::{FP32x32, FP32x32Impl, FixedTrait};
const PRECISION: u64 = 1_000_000;

// TODO: Test append to array version for efficiency

// TODO
fn generate_sine_wave(frequency_hz: u32, duration_ms: u32, sample_rate_hz: u32) -> Array<u8> {
let samples = array![];
let mut samples: Array<u8> = array![];
let num_samples: u32 = ((duration_ms * sample_rate_hz).into() / 1000);
let mut num_samples_left: u32 = ((duration_ms * sample_rate_hz).into() / 1000); //12,000

let denum: FP32x32 = FixedTrait::new_unscaled(1000 * num_samples_left.into(), false);
let PI: FP32x32 = FixedTrait::new(13493037705, false);
loop {
if num_samples_left == 0 {
break;
}
let num: FP32x32 = FixedTrait::new_unscaled(
(num_samples.into() - num_samples_left.into()) * duration_ms.into(), false
);

let t: FP32x32 = num / denum / FixedTrait::new_unscaled(1000 , false);
let mut fp: FP32x32 = (FixedTrait::new_unscaled((2 * frequency_hz.into()), false) * FixedTrait::new_unscaled(t.mag/1000 , false) * PI) / FixedTrait::new_unscaled(10000 , false);
let mut fp_sample_value: FP32x32 = fp.sin() * FixedTrait::new_unscaled(128, false);


fp_sample_value += FixedTrait::new_unscaled(128, false);
let sample_value: u8 = (fp_sample_value % FixedTrait::new_unscaled(128, false)).try_into().unwrap();
samples.append(sample_value);
num_samples_left -= 1;

};
samples
}

Expand Down
Loading