forked from cline/cline
-
Notifications
You must be signed in to change notification settings - Fork 650
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Play sound effects for notifications and events (#38)
Co-authored-by: HeavenOSK <[email protected]>
- Loading branch information
Showing
14 changed files
with
236 additions
and
2 deletions.
There are no files selected for viewing
Binary file not shown.
Binary file not shown.
Binary file not shown.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,68 @@ | ||
import * as vscode from "vscode" | ||
import * as path from "path" | ||
|
||
/** | ||
* Minimum interval (in milliseconds) to prevent continuous playback | ||
*/ | ||
const MIN_PLAY_INTERVAL = 500 | ||
|
||
/** | ||
* Timestamp of when sound was last played | ||
*/ | ||
let lastPlayedTime = 0 | ||
|
||
/** | ||
* Determine if a file is a WAV file | ||
* @param filepath string | ||
* @returns boolean | ||
*/ | ||
export const isWAV = (filepath: string): boolean => { | ||
return path.extname(filepath).toLowerCase() === ".wav" | ||
} | ||
|
||
let isSoundEnabled = true | ||
|
||
/** | ||
* Set sound configuration | ||
* @param enabled boolean | ||
*/ | ||
export const setSoundEnabled = (enabled: boolean): void => { | ||
isSoundEnabled = enabled | ||
} | ||
|
||
/** | ||
* Play a sound file | ||
* @param filepath string | ||
* @return void | ||
*/ | ||
export const playSound = (filepath: string): void => { | ||
try { | ||
if (!isSoundEnabled) { | ||
return | ||
} | ||
|
||
if (!filepath) { | ||
return | ||
} | ||
|
||
if (!isWAV(filepath)) { | ||
throw new Error("Only wav files are supported.") | ||
} | ||
|
||
const currentTime = Date.now() | ||
if (currentTime - lastPlayedTime < MIN_PLAY_INTERVAL) { | ||
return // Skip playback within minimum interval to prevent continuous playback | ||
} | ||
|
||
const player = require("play-sound")() | ||
player.play(filepath, function (err: any) { | ||
if (err) { | ||
throw new Error("Failed to play sound effect") | ||
} | ||
}) | ||
|
||
lastPlayedTime = currentTime | ||
} catch (error: any) { | ||
vscode.window.showErrorMessage(error.message) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.