Skip to content

Commit

Permalink
feat(playback): add audio playback
Browse files Browse the repository at this point in the history
  • Loading branch information
littledivy committed Nov 3, 2020
1 parent a1714ef commit 1b38634
Show file tree
Hide file tree
Showing 5 changed files with 79 additions and 3 deletions.
4 changes: 2 additions & 2 deletions Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
[package]
name = "deno-audio"
name = "deno_audio"
version = "0.1.0"
authors = ["divy-work <[email protected]>"]
edition = "2018"
Expand All @@ -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"
15 changes: 15 additions & 0 deletions detect.ts
Original file line number Diff line number Diff line change
@@ -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}`;
}
1 change: 0 additions & 1 deletion lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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::<Result<(), ()>>();
std::thread::spawn(move || {
// call type_string
let (_stream, handle) = rodio::OutputStream::try_default().unwrap();
let sink = rodio::Sink::try_new(&handle).unwrap();

Expand Down
Binary file added music.mp3
Binary file not shown.
62 changes: 62 additions & 0 deletions plugin.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
import filename from "./detect.ts";
import { prepare } from "https://deno.land/x/[email protected]/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");

0 comments on commit 1b38634

Please sign in to comment.