diff --git a/Cargo.toml b/Cargo.toml index 0af27ad..be1277c 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -1,5 +1,5 @@ [package] -name = "deno-audio" +name = "deno_audio" version = "0.1.0" authors = ["divy-work "] edition = "2018" @@ -14,4 +14,4 @@ rodio = "0.12.0" serde = { version = "1.0", features = ["derive"] } serde_json = "1.0" futures = "0.3.4" -deno_core = "0.66.0" +deno_core = "0.61.0" diff --git a/detect.ts b/detect.ts new file mode 100644 index 0000000..93d6287 --- /dev/null +++ b/detect.ts @@ -0,0 +1,15 @@ + +export default function filename(filenameBase: string): string { + let filenameSuffix = ".so"; + let filenamePrefix = "lib"; + + if (Deno.build.os === "windows") { + filenameSuffix = ".dll"; + filenamePrefix = ""; + } + if (Deno.build.os === "darwin") { + filenameSuffix = ".dylib"; + } + + return `${filenamePrefix}${filenameBase}${filenameSuffix}`; + } \ No newline at end of file diff --git a/lib.rs b/lib.rs index cd20d70..a0c9562 100644 --- a/lib.rs +++ b/lib.rs @@ -27,7 +27,6 @@ fn op_play(_interface: &mut dyn Interface, zero_copy: &mut [ZeroCopyBuf]) -> Op let fut = async move { let (tx, rx) = futures::channel::oneshot::channel::>(); std::thread::spawn(move || { - // call type_string let (_stream, handle) = rodio::OutputStream::try_default().unwrap(); let sink = rodio::Sink::try_new(&handle).unwrap(); diff --git a/music.mp3 b/music.mp3 new file mode 100644 index 0000000..f130f3f Binary files /dev/null and b/music.mp3 differ diff --git a/plugin.ts b/plugin.ts new file mode 100644 index 0000000..167df6c --- /dev/null +++ b/plugin.ts @@ -0,0 +1,62 @@ +import filename from "./detect.ts"; +import { prepare } from "https://deno.land/x/plugin_prepare@v0.8.0/mod.ts"; + +// @ts-ignore +export const core = Deno.core as { + ops: () => { [key: string]: number }; + setAsyncHandler(rid: number, handler: (response: Uint8Array) => void): void; + dispatch( + rid: number, + msg?: any, + buf?: ArrayBufferView, + ): Uint8Array | undefined; + }; + +const { filenameBase, pluginBase } = { + "filenameBase": "deno_audio", + "pluginBase": + "https://github.com/littledivy/deno_audio/releases/latest/download", +}; + +const isDev = Deno.env.get("DEV"); + +if (isDev) { + // This will be checked against open resources after Plugin.close() + // in runTestClose() below. + const resourcesPre = Deno.resources(); + + const rid = Deno.openPlugin("./target/debug/" + filename(filenameBase)); +} else { + // logger.info(`Downloading latest Autopilot release from Github`); + const pluginId = await prepare({ + name: "deno_audio", + urls: { + darwin: `${pluginBase}/libdeno_audio.dylib`, + windows: `${pluginBase}/deno_audio.dll`, + linux: `${pluginBase}/libdeno_audio.so`, + }, + }); +} + + +const { + play, +} = core.ops(); + +const textDecoder = new TextDecoder(); +const decoder = new TextDecoder(); + + +export async function play_audio(arg: string) { + const encoder = new TextEncoder(); + const view = encoder.encode(arg); + return new Promise((resolve, reject) => { + core.setAsyncHandler(play, (bytes) => { + resolve(textDecoder.decode(bytes)); + }); + core.dispatch(play, view); + }); +} + +await play_audio("music.mp3"); +