Initial commit with a sorta working demo

This commit is contained in:
R Tyler Croy 2020-09-17 13:48:40 -07:00
commit 1428cc3289
No known key found for this signature in database
GPG Key ID: E5C92681BEF6CEA2
14 changed files with 2168 additions and 0 deletions

2
.gitignore vendored Normal file
View File

@ -0,0 +1,2 @@
/target
*.sw*

2068
Cargo.lock generated Normal file

File diff suppressed because it is too large Load Diff

18
Cargo.toml Normal file
View File

@ -0,0 +1,18 @@
[package]
name = "charcuterie"
version = "0.1.0"
authors = ["R. Tyler Croy <rtyler@brokenco.de>"]
edition = "2018"
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
[dependencies]
async-std = { version = "~1.6.1", features = ["attributes"] }
glob = "~0.3.0"
handlebars = { version = "~3.4.0", features = ["dir_source"] }
log = "~0.4.11"
pretty_env_logger = "~0.3.1"
rodio = "~0.11.0"
serde = { version = "~1.0.104", features = ["derive"] }
serde_json = "~1.0.0"
tide = "~0.13.0"

BIN
sounds/applause.wav Normal file

Binary file not shown.

BIN
sounds/boo.wav Normal file

Binary file not shown.

BIN
sounds/cheer.wav Normal file

Binary file not shown.

BIN
sounds/factorywhistle.wav Normal file

Binary file not shown.

BIN
sounds/gasp.wav Normal file

Binary file not shown.

BIN
sounds/giggle.wav Normal file

Binary file not shown.

BIN
sounds/smirk.wav Normal file

Binary file not shown.

BIN
sounds/yawn.wav Normal file

Binary file not shown.

BIN
sounds/yeehaw.wav Normal file

Binary file not shown.

60
src/main.rs Normal file
View File

@ -0,0 +1,60 @@
#[macro_use]
extern crate serde_json;
use async_std::task;
use glob::glob;
use handlebars::Handlebars;
use log::*;
use rodio::Source;
use tide::{Body, Request, StatusCode};
use std::fs::File;
use std::io::BufReader;
async fn index(req: Request<()>) -> Result<Body, tide::Error> {
let mut hb = Handlebars::new();
hb.register_templates_directory(".hbs", "views")
.expect("Failed to register templates");
let mut sounds = vec![];
for sound in glob("sounds/*.wav").expect("Failed to glob sounds") {
if let Ok(sound) = sound {
if let Some(name) = sound.as_path().file_stem() {
sounds.push(name.to_os_string().into_string().unwrap());
}
}
}
info!("sounds: {:?}", sounds);
let view = hb.render("index", &json!({"sounds": sounds}))
.expect("Failed to render");
let mut body = Body::from_string(view);
body.set_mime("text/html");
Ok(body)
}
async fn play(req: Request<()>) -> tide::Result {
if let Ok(sound) = req.param::<String>("sound") {
let device = rodio::default_output_device().unwrap();
let file = File::open(format!("sounds/{}.wav", sound)).unwrap();
let source = rodio::Decoder::new(BufReader::new(file)).unwrap();
rodio::play_raw(&device, source.convert_samples());
Ok(tide::Redirect::new("/").into())
}
else {
Err(tide::Error::from_str(StatusCode::NotFound, "Could not find sound"))
}
}
#[async_std::main]
async fn main() -> Result<(), tide::Error> {
pretty_env_logger::init();
let mut app = tide::new();
app.at("/play/:sound").post(play);
app.at("/").get(index);
Ok(app.listen("0.0.0.0:9077").await?)
}

20
views/index.hbs Normal file
View File

@ -0,0 +1,20 @@
<html>
<head>
<title>Charcuterie</title>
</head>
<body>
A sound board for Meet, get it?
{{#each sounds}}
<form action="/play/{{this}}" method="POST">
<input type="submit" value="{{this}}"/>
</form>
{{/each}}
</body>
</html>
<!--
vim: ft=html
-->