Add conversion command for midi files
All checks were successful
Build and push container image / build (push) Successful in 17m19s
All checks were successful
Build and push container image / build (push) Successful in 17m19s
This commit is contained in:
parent
13c83591d7
commit
890550c6ef
21
Cargo.lock
generated
21
Cargo.lock
generated
@ -972,7 +972,7 @@ dependencies = [
|
|||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "kon"
|
name = "kon"
|
||||||
version = "0.3.0"
|
version = "0.3.1"
|
||||||
dependencies = [
|
dependencies = [
|
||||||
"bb8",
|
"bb8",
|
||||||
"bb8-postgres",
|
"bb8-postgres",
|
||||||
@ -980,6 +980,7 @@ dependencies = [
|
|||||||
"once_cell",
|
"once_cell",
|
||||||
"os_info",
|
"os_info",
|
||||||
"poise",
|
"poise",
|
||||||
|
"regex",
|
||||||
"reqwest 0.12.5",
|
"reqwest 0.12.5",
|
||||||
"serde",
|
"serde",
|
||||||
"serde_json",
|
"serde_json",
|
||||||
@ -1350,11 +1351,11 @@ dependencies = [
|
|||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "postgres-protocol"
|
name = "postgres-protocol"
|
||||||
version = "0.6.6"
|
version = "0.6.7"
|
||||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
checksum = "49b6c5ef183cd3ab4ba005f1ca64c21e8bd97ce4699cfea9e8d9a2c4958ca520"
|
checksum = "acda0ebdebc28befa84bee35e651e4c5f09073d668c7aed4cf7e23c3cda84b23"
|
||||||
dependencies = [
|
dependencies = [
|
||||||
"base64 0.21.7",
|
"base64 0.22.1",
|
||||||
"byteorder",
|
"byteorder",
|
||||||
"bytes",
|
"bytes",
|
||||||
"fallible-iterator",
|
"fallible-iterator",
|
||||||
@ -1368,9 +1369,9 @@ dependencies = [
|
|||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "postgres-types"
|
name = "postgres-types"
|
||||||
version = "0.2.6"
|
version = "0.2.7"
|
||||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
checksum = "8d2234cdee9408b523530a9b6d2d6b373d1db34f6a8e51dc03ded1828d7fb67c"
|
checksum = "02048d9e032fb3cc3413bbf7b83a15d84a5d419778e2628751896d856498eee9"
|
||||||
dependencies = [
|
dependencies = [
|
||||||
"bytes",
|
"bytes",
|
||||||
"fallible-iterator",
|
"fallible-iterator",
|
||||||
@ -1494,9 +1495,9 @@ dependencies = [
|
|||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "regex"
|
name = "regex"
|
||||||
version = "1.10.4"
|
version = "1.10.5"
|
||||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
checksum = "c117dbdfde9c8308975b6a18d71f3f385c89461f7b3fb054288ecf2a2058ba4c"
|
checksum = "b91213439dad192326a0d7c6ee3955910425f441d7038e0d6933b0aec5c4517f"
|
||||||
dependencies = [
|
dependencies = [
|
||||||
"aho-corasick",
|
"aho-corasick",
|
||||||
"memchr",
|
"memchr",
|
||||||
@ -2218,9 +2219,9 @@ dependencies = [
|
|||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "tokio-postgres"
|
name = "tokio-postgres"
|
||||||
version = "0.7.10"
|
version = "0.7.11"
|
||||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
checksum = "d340244b32d920260ae7448cb72b6e238bddc3d4f7603394e7dd46ed8e48f5b8"
|
checksum = "03adcf0147e203b6032c0b2d30be1415ba03bc348901f3ff1cc0df6a733e60c3"
|
||||||
dependencies = [
|
dependencies = [
|
||||||
"async-trait",
|
"async-trait",
|
||||||
"byteorder",
|
"byteorder",
|
||||||
|
@ -1,6 +1,6 @@
|
|||||||
[package]
|
[package]
|
||||||
name = "kon"
|
name = "kon"
|
||||||
version = "0.3.0"
|
version = "0.3.1"
|
||||||
edition = "2021"
|
edition = "2021"
|
||||||
|
|
||||||
[dependencies]
|
[dependencies]
|
||||||
@ -10,13 +10,14 @@ cargo_toml = "0.20.4"
|
|||||||
once_cell = "1.19.0"
|
once_cell = "1.19.0"
|
||||||
os_info = "3.8.2"
|
os_info = "3.8.2"
|
||||||
poise = "0.6.1"
|
poise = "0.6.1"
|
||||||
|
regex = "1.10.5"
|
||||||
reqwest = { version = "0.12.5", features = ["json"] }
|
reqwest = { version = "0.12.5", features = ["json"] }
|
||||||
serde = "1.0.204"
|
serde = "1.0.204"
|
||||||
serde_json = "1.0.120"
|
serde_json = "1.0.120"
|
||||||
sysinfo = "0.30.13"
|
sysinfo = "0.30.13"
|
||||||
tokenservice-client = { version = "0.3.2", registry = "gitea" }
|
tokenservice-client = { version = "0.3.2", registry = "gitea" }
|
||||||
tokio = { version = "1.38.1", features = ["macros", "signal", "rt-multi-thread"] }
|
tokio = { version = "1.38.1", features = ["macros", "signal", "rt-multi-thread"] }
|
||||||
tokio-postgres = "0.7.10"
|
tokio-postgres = "0.7.11"
|
||||||
uptime_lib = "0.3.1"
|
uptime_lib = "0.3.1"
|
||||||
|
|
||||||
[features]
|
[features]
|
||||||
|
@ -18,7 +18,7 @@ COPY . .
|
|||||||
RUN cargo build -rF production
|
RUN cargo build -rF production
|
||||||
|
|
||||||
FROM alpine:3.20
|
FROM alpine:3.20
|
||||||
RUN apk add --no-cache libgcc
|
RUN apk add --no-cache libgcc fluidsynth
|
||||||
WORKDIR /kon
|
WORKDIR /kon
|
||||||
COPY --from=builder /usr/src/kon/target/release/kon .
|
COPY --from=builder /usr/src/kon/target/release/kon .
|
||||||
CMD [ "./kon" ]
|
CMD [ "./kon" ]
|
||||||
|
@ -1,3 +1,4 @@
|
|||||||
pub mod ping;
|
pub mod ping;
|
||||||
pub mod status;
|
pub mod status;
|
||||||
|
pub mod midi;
|
||||||
pub mod uptime;
|
pub mod uptime;
|
||||||
|
78
src/commands/midi.rs
Normal file
78
src/commands/midi.rs
Normal file
@ -0,0 +1,78 @@
|
|||||||
|
use crate::{
|
||||||
|
Error,
|
||||||
|
internals::http::HttpClient
|
||||||
|
};
|
||||||
|
|
||||||
|
use regex::Regex;
|
||||||
|
use std::fs::{
|
||||||
|
write,
|
||||||
|
read_to_string,
|
||||||
|
remove_file
|
||||||
|
};
|
||||||
|
use poise::{
|
||||||
|
CreateReply,
|
||||||
|
serenity_prelude::CreateAttachment
|
||||||
|
};
|
||||||
|
|
||||||
|
/// Convert MIDI file to WAV
|
||||||
|
#[poise::command(context_menu_command = "MIDI -> WAV")]
|
||||||
|
pub async fn midi_to_wav(
|
||||||
|
ctx: poise::Context<'_, (), Error>,
|
||||||
|
#[description = "MIDI file to be converted"] message: poise::serenity_prelude::Message
|
||||||
|
) -> Result<(), Error> {
|
||||||
|
ctx.defer().await?;
|
||||||
|
|
||||||
|
let http = HttpClient::new();
|
||||||
|
let resp = http.get(&message.attachments[0].url, "MIDI Conversion").await?;
|
||||||
|
let bytes = resp.bytes().await?;
|
||||||
|
|
||||||
|
let midi_path = &message.attachments[0].filename;
|
||||||
|
write(midi_path, bytes)?;
|
||||||
|
|
||||||
|
let re = Regex::new(r"(?i)\.mid$").unwrap();
|
||||||
|
|
||||||
|
let wav_path = re.replace(&midi_path, ".wav");
|
||||||
|
|
||||||
|
let alpine_sf2 = include_bytes!("../internals/assets/FluidR3_GM.sf2");
|
||||||
|
let sf2_path = if let Ok(os_release) = read_to_string("/etc/os-release") {
|
||||||
|
if os_release.contains("Alpine") {
|
||||||
|
let sf2_path = "/tmp/FluidR3_GM.sf2";
|
||||||
|
write(sf2_path, alpine_sf2)?;
|
||||||
|
sf2_path
|
||||||
|
} else {
|
||||||
|
"/usr/share/sounds/sf2/FluidR3_GM.sf2"
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
return Err(Error::from("Couldn't read \"/etc/os-release\" file!"))
|
||||||
|
};
|
||||||
|
|
||||||
|
let output = std::process::Command::new("fluidsynth")
|
||||||
|
.args(&[
|
||||||
|
"-ni", sf2_path, midi_path, "-F", &wav_path
|
||||||
|
])
|
||||||
|
.output();
|
||||||
|
|
||||||
|
match output {
|
||||||
|
Ok(_) => {
|
||||||
|
ctx.send(CreateReply::default()
|
||||||
|
.attachment(CreateAttachment::path(&*wav_path).await.unwrap())
|
||||||
|
).await.expect("Reply failed");
|
||||||
|
|
||||||
|
remove_file(midi_path)?;
|
||||||
|
remove_file(&*wav_path)?;
|
||||||
|
},
|
||||||
|
Err(y) => {
|
||||||
|
ctx.send(CreateReply::default()
|
||||||
|
.content("Command didn't execute successfully, check console for more information!")
|
||||||
|
)
|
||||||
|
.await.unwrap();
|
||||||
|
|
||||||
|
return Err(Error::from(format!(
|
||||||
|
"Midi conversion failed: {}",
|
||||||
|
y
|
||||||
|
)))
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
Ok(())
|
||||||
|
}
|
BIN
src/internals/assets/FluidR3_GM.sf2
Normal file
BIN
src/internals/assets/FluidR3_GM.sf2
Normal file
Binary file not shown.
@ -98,6 +98,7 @@ async fn main() {
|
|||||||
commands: vec![
|
commands: vec![
|
||||||
commands::ping::ping(),
|
commands::ping::ping(),
|
||||||
commands::status::status(),
|
commands::status::status(),
|
||||||
|
commands::midi::midi_to_wav(),
|
||||||
commands::uptime::uptime()
|
commands::uptime::uptime()
|
||||||
],
|
],
|
||||||
pre_command: |ctx| Box::pin(async move {
|
pre_command: |ctx| Box::pin(async move {
|
||||||
|
Loading…
Reference in New Issue
Block a user