Skip to content

Commit

Permalink
Merge pull request #10 from nbennett320/feature/state
Browse files Browse the repository at this point in the history
Document model V0 + UI/Engine Syncing with Redux + Project structuring
  • Loading branch information
nbennett320 committed Apr 8, 2022
2 parents 02ca492 + a06e665 commit 166dc52
Show file tree
Hide file tree
Showing 38 changed files with 1,150 additions and 769 deletions.
9 changes: 9 additions & 0 deletions .eslintrc.cjs
Expand Up @@ -33,7 +33,16 @@ module.exports = {
},
],
'@typescript-eslint/naming-convention': 'off',
'@typescript-eslint/no-empty-interface': 'off',
'arrow-body-style': 'warn',
'import/no-cycle': 'off',
'import/prefer-default-export': 'off',
'no-param-reassign': 'off',
'react/button-has-type': 'off',
'react/destructuring-assignment': 'off',
'react/function-component-definition': 'off',
'react/jsx-props-no-spreading': 'off',
'react/require-default-props': 'off',
'@typescript-eslint/no-loss-of-precision': 'off',
},
}
9 changes: 7 additions & 2 deletions .github/workflows/build.yaml
@@ -1,5 +1,10 @@
name: build app
on: [pull_request]
name: Build App

on:
pull_request:
branches:
- main

jobs:
build:
strategy:
Expand Down
33 changes: 33 additions & 0 deletions .github/workflows/lint.yaml
@@ -0,0 +1,33 @@
name: Linting

on: push

jobs:
# rust_lint:
# runs-on: ubuntu-latest
# steps:
# - uses: actions/checkout@v1
# - name: Install dependencies (ubuntu only)
# run: |
# sudo apt-get update
# sudo apt-get install -y libgtk-3-dev webkit2gtk-4.0 libappindicator3-dev librsvg2-dev patchelf libasound2-dev
# - run: rustup component add clippy
# - name: Rust Clippy
# run: cd ./backend/ && cargo clippy --all-targets --all-features -- -D warnings

typescript_lint:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v2
- uses: pnpm/action-setup@v2.0.1
with:
version: 6.20.3
- name: Use Node.js 16
uses: actions/setup-node@v2
with:
node-version: 16
cache: 'pnpm'
- name: Install node modules
run: pnpm install
- name: Lint typescript
run: pnpm lint
2 changes: 1 addition & 1 deletion .prettierrc.cjs
@@ -1,6 +1,6 @@
module.exports = {
endOfLine: 'lf',
printWidth: 120,
printWidth: 80,
semi: false,
singleQuote: true,
tabWidth: 2,
Expand Down
38 changes: 25 additions & 13 deletions backend/Cargo.toml
@@ -1,31 +1,43 @@
[package]
name = "app"
version = "0.1.0"
description = "A Tauri App"
authors = ["you"]
license = ""
repository = ""
default-run = "app"
description = "dawesome/engine"
edition = "2021"
rust-version = "1.57"
license = "MIT"
name = "app"
repository = "https://github.com/nbennett320/dawesome"
rust-version = "1.59"
version = "0.1.0"

# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html

[build-dependencies]
tauri-build = { version = "1.0.0-rc.5", features = [] }
tauri-build = {version = "1.0.0-rc.5", features = []}

[dependencies]
serde_json = "1.0"
serde = { version = "1.0", features = ["derive"] }
tauri = { version = "1.0.0-rc.5", features = ["api-all"] }
rodio = { version = "*" }
chrono = "0.4"
futures = "0.3.21"
rodio = {version = "*"}
serde = {version = "1.0", features = ["derive"]}
serde_json = "1.0"
tauri = {version = "1.0.0-rc.5", features = ["api-all"]}

[features]
# by default Tauri runs in production mode
# when `tauri dev` runs it is executed with `cargo run --no-default-features` if `devPath` is an URL
default = [ "custom-protocol" ]
default = ["custom-protocol"]
# this feature is used used for production builds where `devPath` points to the filesystem
# DO NOT remove this
custom-protocol = [ "tauri/custom-protocol" ]
custom-protocol = ["tauri/custom-protocol"]

[profile.release]
codegen-units = 1
debug = 0
incremental = false
lto = true
opt-level = 3
overflow-checks = false
panic = "abort"

# unsupported optimiaztion for rust versions < 1.59
strip = "debuginfo"
2 changes: 1 addition & 1 deletion backend/build.rs
@@ -1,3 +1,3 @@
fn main() {
tauri_build::build()
tauri_build::build()
}
5 changes: 5 additions & 0 deletions backend/rustfmt.toml
@@ -0,0 +1,5 @@
fn_args_layout = "Vertical"
max_width = 80
tab_spaces = 2
use_field_init_shorthand = true
use_try_shorthand = true
20 changes: 9 additions & 11 deletions backend/src/daw/daw.rs
@@ -1,15 +1,12 @@
use rodio::{Decoder, OutputStream, Sink};
use std::fs::File;
use std::io::BufReader;
use std::thread;
use std::sync::atomic;
use std::sync;
use std::sync::atomic;
use std::thread;
use std::time;
use futures;
use crate::daw::{
state,
daw_core,
};

use crate::daw::{daw_core, state};

pub async fn play_sample(path: &str) {
let path = String::from(path);
Expand All @@ -31,15 +28,16 @@ pub fn run_metronome(state_ref: &sync::Arc<state::InnerState>) {
let tempo = *state_ref.global_tempo_bpm.lock().unwrap();
let tempo_intrv_ms = daw_core::timing::tempo_to_intrv_ms(tempo);


pool.exec(move || {
thread::spawn(move || {
loop {
// let timer = timer::Timer::new();
// let timer = timer::Timer::new();
// timer.timeout(time::Duration::new((tempo / 60) as u64, 0));

println!("tick");
futures::executor::block_on(play_sample("assets/assets_66-hh-01-or.wav"));
futures::executor::block_on(play_sample(
"assets/assets_66-hh-01-or.wav",
));
thread::sleep(time::Duration::from_millis(tempo_intrv_ms));
if !state.playlist_is_playing.load(atomic::Ordering::SeqCst) {
break;
Expand All @@ -49,4 +47,4 @@ pub fn run_metronome(state_ref: &sync::Arc<state::InnerState>) {
});

println!("continuing");
}
}
42 changes: 21 additions & 21 deletions backend/src/daw/daw_core/threadpool.rs
@@ -1,6 +1,6 @@
use std::thread;
use std::sync;
use std::sync::mpsc;
use std::thread;
use std::vec;

trait FnBox {
Expand All @@ -21,27 +21,25 @@ enum Message {
}

struct Worker {
id: usize,
_id: usize,
thread: Option<thread::JoinHandle<()>>,
}

impl Worker {
fn new(
id: usize,
revc: sync::Arc<sync::Mutex<mpsc::Receiver<Message>>>
_id: usize,
revc: sync::Arc<sync::Mutex<mpsc::Receiver<Message>>>,
) -> Self {
let thread = thread::spawn(move || {
loop {
let msg = revc.lock().unwrap().recv().unwrap();
match msg {
Message::NewJob(job) => job.call(),
Message::Kill => break,
}
let thread = thread::spawn(move || loop {
let msg = revc.lock().unwrap().recv().unwrap();
match msg {
Message::NewJob(job) => job.call(),
Message::Kill => break,
}
});

Worker {
id,
Worker {
_id,
thread: Some(thread),
}
}
Expand All @@ -56,22 +54,24 @@ impl ThreadPool {
pub fn new(size: usize) -> Self {
assert!(size > 0);

let (sender, rx): (mpsc::Sender<Message>, mpsc::Receiver<Message>) = mpsc::channel();
let (sender, rx): (mpsc::Sender<Message>, mpsc::Receiver<Message>) =
mpsc::channel();
let recv = sync::Arc::new(sync::Mutex::new(rx));
let mut workers = vec::Vec::with_capacity(size);

for id in 0..size {
workers.push(Worker::new(id, sync::Arc::clone(&recv)));
}

ThreadPool {
workers,
sender,
}
ThreadPool { workers, sender }
}

pub fn exec<F>(&self, f: F)
where F: FnOnce() + Send + 'static {
pub fn exec<F>(
&self,
f: F,
) where
F: FnOnce() + Send + 'static,
{
let job = Box::new(f);
self.sender.send(Message::NewJob(job)).unwrap();
}
Expand All @@ -82,7 +82,7 @@ impl Drop for ThreadPool {
for _ in &mut self.workers {
self.sender.send(Message::Kill).unwrap();
}

for worker in &mut self.workers {
if let Some(thread) = worker.thread.take() {
thread.join().unwrap();
Expand Down
59 changes: 27 additions & 32 deletions backend/src/daw/daw_core/timer.rs
@@ -1,9 +1,9 @@
use std::future;
use std::pin;
use std::sync;
use std::task;
use std::thread;
use std::time;
use std::task;
use std::future;
use std::pin;

struct TimerBaseState {
timed_out: bool,
Expand All @@ -12,37 +12,34 @@ struct TimerBaseState {

struct TimerBase {
delay: time::Duration,
handle: Option<thread::JoinHandle<()>>,
state: sync::Arc<sync::Mutex<TimerBaseState>>
_handle: Option<thread::JoinHandle<()>>,
state: sync::Arc<sync::Mutex<TimerBaseState>>,
}

impl TimerBase {
fn new(delay: time::Duration) -> Self {
fn _new(delay: time::Duration) -> Self {
TimerBase {
delay: delay,
handle: None,
state: sync::Arc::new(
sync::Mutex::new(
TimerBaseState {
timed_out: false,
already_polled: false,
}
)
)
delay,
_handle: None,
state: sync::Arc::new(sync::Mutex::new(TimerBaseState {
timed_out: false,
already_polled: false,
})),
}
}

fn spawn_timer_thread(
&self,
context: &mut task::Context<'_>,
&self,
context: &mut task::Context<'_>,
) {
let delay = self.delay;
let state = sync::Arc::clone(&self.state);
let waker_handle = context.waker().clone();

thread::spawn(move || {
thread::sleep(delay);
let mut state_handle = state.lock().expect("Can't lock the state in timer thread");
let mut state_handle =
state.lock().expect("Can't lock the state in timer thread");
(*state_handle).timed_out = true;
waker_handle.wake();
});
Expand All @@ -53,8 +50,8 @@ impl future::Future for TimerBase {
type Output = ();

fn poll(
self: pin::Pin<&mut Self>,
context: &mut task::Context<'_>
self: pin::Pin<&mut Self>,
context: &mut task::Context<'_>,
) -> task::Poll<Self::Output> {
let mut state = self.state.lock().expect("Can't lock timer state");

Expand All @@ -70,23 +67,21 @@ impl future::Future for TimerBase {
}
}

fn spawn_timeout(delay: time::Duration) -> impl future::Future {
TimerBase::new(delay)
fn _spawn_timeout(delay: time::Duration) -> impl future::Future {
TimerBase::_new(delay)
}

pub struct Timer {}
pub struct _Timer {}

impl Timer {
pub fn new() -> Self {
Timer {
}
impl _Timer {
pub fn _new() -> Self {
_Timer {}
}

pub fn timeout(
pub fn _timeout(
&self,
delay: time::Duration
delay: time::Duration,
) -> impl future::Future {
let timer = spawn_timeout(delay);
timer
_spawn_timeout(delay)
}
}
2 changes: 1 addition & 1 deletion backend/src/daw/state/state.rs
Expand Up @@ -9,7 +9,7 @@ pub struct InnerState {
}

impl InnerState {
pub fn new() -> Option<Self> {
pub fn _new() -> Option<Self> {
None
}

Expand Down

0 comments on commit 166dc52

Please sign in to comment.