diff --git a/package-lock.json b/package-lock.json index a55262e..4db1623 100644 --- a/package-lock.json +++ b/package-lock.json @@ -537,6 +537,11 @@ "pend": "~1.2.0" } }, + "file-type": { + "version": "10.11.0", + "resolved": "https://registry.npmjs.org/file-type/-/file-type-10.11.0.tgz", + "integrity": "sha512-uzk64HRpUZyTGZtVuvrjP0FYxzQrBf4rojot6J65YMEbwBLB0CWm0CLojVpwpmFmxcE/lkvYICgfcGozbBq6rw==" + }, "flush-write-stream": { "version": "1.1.1", "resolved": "https://registry.npmjs.org/flush-write-stream/-/flush-write-stream-1.1.1.tgz", @@ -1126,6 +1131,11 @@ "integrity": "sha1-5WqpTEyAVaFkBKBnS3jyFffI4ZQ=", "dev": true }, + "media-typer": { + "version": "0.3.0", + "resolved": "https://registry.npmjs.org/media-typer/-/media-typer-0.3.0.tgz", + "integrity": "sha1-hxDXrwqmJvj/+hzgAWhUUmMlV0g=" + }, "mime-db": { "version": "1.38.0", "resolved": "https://registry.npmjs.org/mime-db/-/mime-db-1.38.0.tgz", @@ -1244,6 +1254,33 @@ "minimatch": "^3.0.0" } }, + "music-metadata": { + "version": "3.5.4", + "resolved": "https://registry.npmjs.org/music-metadata/-/music-metadata-3.5.4.tgz", + "integrity": "sha512-28MDswjKQRVQEv355ROZ2ks/iVwds/+TGbnqOjO8A4BxlNZ+R9nDULw0dbFzOpz7tujtB0mXCINd/JC2Kua6sw==", + "requires": { + "debug": "^4.1.0", + "file-type": "^10.5.0", + "media-typer": "0.3.0", + "strtok3": "^2.3.0", + "token-types": "^1.0.1" + }, + "dependencies": { + "debug": { + "version": "4.1.1", + "resolved": "https://registry.npmjs.org/debug/-/debug-4.1.1.tgz", + "integrity": "sha512-pYAIzeRo8J6KPEaJ0VWOh5Pzkbw/RetuzehGM7QRRX5he4fPHx2rdKMB256ehJCkX+XRQm16eZLqLNS8RSZXZw==", + "requires": { + "ms": "^2.1.1" + } + }, + "ms": { + "version": "2.1.1", + "resolved": "https://registry.npmjs.org/ms/-/ms-2.1.1.tgz", + "integrity": "sha512-tgp+dl5cGk28utYktBsrFqA7HKgrhgPsg6Z/EfhWI4gl1Hwq8B/GmY/0oXZ6nF8hDVesS/FpnYaD/kOWhYQvyg==" + } + } + }, "node-wav-player": { "version": "0.1.0", "resolved": "https://registry.npmjs.org/node-wav-player/-/node-wav-player-0.1.0.tgz", @@ -1674,6 +1711,31 @@ "ansi-regex": "^2.0.0" } }, + "strtok3": { + "version": "2.3.0", + "resolved": "https://registry.npmjs.org/strtok3/-/strtok3-2.3.0.tgz", + "integrity": "sha512-AA67/1atBh7X0fUTDevjW89by2ZkY9RZAnkwusx5Yc1COYf0ruUbpYOOIs03SnRA1CF9K3+BtRXKOEtKhAXVaQ==", + "requires": { + "debug": "^4.1.0", + "then-read-stream": "^1.5.0", + "token-types": "^1.0.1" + }, + "dependencies": { + "debug": { + "version": "4.1.1", + "resolved": "https://registry.npmjs.org/debug/-/debug-4.1.1.tgz", + "integrity": "sha512-pYAIzeRo8J6KPEaJ0VWOh5Pzkbw/RetuzehGM7QRRX5he4fPHx2rdKMB256ehJCkX+XRQm16eZLqLNS8RSZXZw==", + "requires": { + "ms": "^2.1.1" + } + }, + "ms": { + "version": "2.1.1", + "resolved": "https://registry.npmjs.org/ms/-/ms-2.1.1.tgz", + "integrity": "sha512-tgp+dl5cGk28utYktBsrFqA7HKgrhgPsg6Z/EfhWI4gl1Hwq8B/GmY/0oXZ6nF8hDVesS/FpnYaD/kOWhYQvyg==" + } + } + }, "supports-color": { "version": "2.0.0", "resolved": "https://registry.npmjs.org/supports-color/-/supports-color-2.0.0.tgz", @@ -1691,6 +1753,11 @@ "inherits": "2" } }, + "then-read-stream": { + "version": "1.5.0", + "resolved": "https://registry.npmjs.org/then-read-stream/-/then-read-stream-1.5.0.tgz", + "integrity": "sha512-Qu+Dp3G28GBmewssIjoRwcmdnsymjdSFt17s8qAajQKesBjLoRAj3VgWftiLSC5CJV6PEYOS0d45WGTJp8QFxQ==" + }, "through": { "version": "2.3.8", "resolved": "https://registry.npmjs.org/through/-/through-2.3.8.tgz", @@ -1736,6 +1803,11 @@ "through2": "^2.0.3" } }, + "token-types": { + "version": "1.0.1", + "resolved": "https://registry.npmjs.org/token-types/-/token-types-1.0.1.tgz", + "integrity": "sha512-Q2yPT4GA4gmIAgUrL9O9LRzKFKJ8FA1P5eiOfC2eH+IZMSJVQBCML1SOooeRicIIvTr/UZuTAX5PqMzgBXJOvg==" + }, "tough-cookie": { "version": "2.4.3", "resolved": "https://registry.npmjs.org/tough-cookie/-/tough-cookie-2.4.3.tgz", diff --git a/package.json b/package.json index d47d4fc..a7fd6b1 100644 --- a/package.json +++ b/package.json @@ -114,6 +114,7 @@ "@types/mocha": "^2.2.42" }, "dependencies": { + "music-metadata": "^3.5.4", "node-wav-player": "^0.1.0", "path": "^0.12.7" } diff --git a/src/extension.ts b/src/extension.ts index 79f2e8d..bd612ac 100644 --- a/src/extension.ts +++ b/src/extension.ts @@ -1,6 +1,7 @@ // The module 'vscode' contains the VS Code extensibility API // Import the module and reference it with the alias vscode in your code below import * as vscode from "vscode"; +import * as mm from 'music-metadata'; const wavPlayer = require("node-wav-player"); const path = require("path"); @@ -13,52 +14,73 @@ export function activate(context: vscode.ExtensionContext) { console.log( 'Congratulations, your extension "codechampion-vscode" is now active!' ); + + let statusMessage: any | undefined = []; const state = { isPlaying: false }; - function playSound(winOrFail: string) { - var isWin = winOrFail === "win"; + async function playSound(winOrFail: string) { + let isWin = (winOrFail === 'win'); - var configs = vscode.workspace.getConfiguration(); - - var soundFileNameInConfig: any | undefined = " "; - soundFileNameInConfig = isWin + let configs = vscode.workspace.getConfiguration(); + + let soundFileNameInConfig: any | undefined = " "; + soundFileNameInConfig = isWin ? configs.get("codechampion.victorySoundConfig") : configs.get("codechampion.failSoundConfig"); - console.log('object', configs.get("codechampion.victorySoundConfig")) - var soundFileName = soundFileNameInConfig.split(" ").join("_") + ".wav"; + let soundFileName = + soundFileNameInConfig.split(" ").join("_") + ".wav"; + - var soundFilePath = path.join( + let soundFilePath = path.join( __dirname, "..", "sounds", winOrFail, soundFileName ); - state.isPlaying = true + + let duration:number | undefined; + await mm.parseFile(soundFilePath) + .then(metadata => { + duration = metadata.format.duration; + }) + .catch((err) => { + console.error(err.message); + }); + + state.isPlaying = true; wavPlayer .play({ path: soundFilePath, sync: true }) .then(() => { - state.isPlaying = false + state.isPlaying = false; console.log("The wav file started to be played successfully."); - }).catch(() => { - state.isPlaying = true }) + .catch((error: any) => { + console.error(error); + state.isPlaying = true; + }); + return new Promise(resolve => { + resolve(duration); + }); } let playVictorySound = vscode.commands.registerCommand( "extension.playVictorySound", () => { if (state.isPlaying) { - vscode.window.setStatusBarMessage("Stahp pushing that button!", 2000); + const message = vscode.window.setStatusBarMessage("Stahp pushing that button!", 2000); + statusMessage.push(message); } else { - vscode.window.setStatusBarMessage("Congratulations!", 2000); - - playSound("win"); + playSound('win') + .then((duration: any) => { + const message = vscode.window.setStatusBarMessage("Congratulations!", duration * 1000); + statusMessage.push(message); + }); } } ); @@ -67,19 +89,30 @@ export function activate(context: vscode.ExtensionContext) { "extension.playFailSound", () => { if (state.isPlaying) { - vscode.window.setStatusBarMessage("Stahp pushing that button!", 2000); + const message = vscode.window.setStatusBarMessage("Stahp pushing that button!", 2000); + statusMessage.push(message); } else { - vscode.window.setStatusBarMessage("It's Ok, Don't worry!", 2000); - - playSound("fail"); + playSound('fail') + .then((duration: any) => { + console.log(duration); + const message = vscode.window.setStatusBarMessage("It's Ok, Don't worry!", duration * 1000); + statusMessage.push(message); + }); } } ); - let stopSound = vscode.commands.registerCommand("extension.stopSound", () => { - wavPlayer.stop(); - state.isPlaying = false - }); + let stopSound = vscode.commands.registerCommand( + "extension.stopSound", + () => { + wavPlayer.stop(); + state.isPlaying = false; + statusMessage.forEach((message: any) => { + message.dispose(); + }); + } + ); + context.subscriptions.push(playVictorySound); context.subscriptions.push(playFailSound);