Skip to content

Commit

Permalink
add: option to disable node-libsamplerate usage
Browse files Browse the repository at this point in the history
This commit allows to disable "node-libsamplerate" usage when impossible to use it.
  • Loading branch information
ThePedroo committed May 20, 2024
1 parent 257a116 commit d8da381
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 11 deletions.
3 changes: 3 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,9 @@ $ npm i
> [!NOTE]
> If you want to use pure JavaScript, replace `sodium-native` with `libsodium-wrappers`. Keep in mind that pure JavaScript will offer a worse performance.
> [!NOTE]
> If you are incapable of installing `node-libsamplerate`, remove its dependency from `package.json` and disable `nativePlayback` in `config.js`.
### 3. Run NodeLink

```shell
Expand Down
3 changes: 2 additions & 1 deletion config.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,8 @@ export default {
maxSearchResults: 200,
maxAlbumPlaylistLength: 200,
maxCaptionsLength: 3,
logFile: 'logs.txt'
logFile: 'logs.txt',
nativePlayback: true
},
debug: {
youtube: {
Expand Down
28 changes: 18 additions & 10 deletions src/voice/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,19 +5,27 @@ const require = createRequire(import.meta.url)
let lame = null
if (!process.isBun) lame = require('@flat/lame') /* libmp3lame bindings */

import SampleRate from 'node-libsamplerate' /* libsamplerate bindings */

import config from '../../config.js'
import constants from '../../constants.js'

let SampleRate = null
let resamplingQuality = null
switch (config.audio.resamplingQuality) {
case 'best': resamplingQuality = SampleRate.SRC_SINC_BEST_QUALITY; break
case 'medium': resamplingQuality = SampleRate.SRC_SINC_MEDIUM_QUALITY; break
case 'fastest': resamplingQuality = SampleRate.SRC_ZERO_ORDER_HOLD; break
case 'zero order holder': resamplingQuality = SampleRate.SRC_ZERO_ORDER_HOLD; break
case 'linear': resamplingQuality = SampleRate.SRC_LINEAR; break

async function _startUp() {
if (config.options.nativePlayback) {
SampleRate = await import('node-libsamplerate')
SampleRate = SampleRate.default

switch (config.audio.resamplingQuality) {
case 'best': resamplingQuality = SampleRate.SRC_SINC_BEST_QUALITY; break
case 'medium': resamplingQuality = SampleRate.SRC_SINC_MEDIUM_QUALITY; break
case 'fastest': resamplingQuality = SampleRate.SRC_ZERO_ORDER_HOLD; break
case 'zero order holder': resamplingQuality = SampleRate.SRC_ZERO_ORDER_HOLD; break
case 'linear': resamplingQuality = SampleRate.SRC_LINEAR; break
}
}
}
_startUp()

class NodeLinkStream {
constructor(stream, pipes, ffmpegState) {
Expand Down Expand Up @@ -111,7 +119,7 @@ function isDecodedInternally(stream, type) {
case 'webm/opus':
case 'ogg/opus': return 3 + 1 + (stream.ffmpegState === 2 ? -2 : 0)
case 'wav': return 2 + 1 + (stream.ffmpegState === 2 ? -2 : 0)
case 'mp3': return lame ? 2 + 1 + (stream.ffmpegState === 2 ? -2 : 0) : false
case 'mp3': return lame && SampleRate ? 2 + 1 + (stream.ffmpegState === 2 ? -2 : 0) : false
default: return false
}
}
Expand Down Expand Up @@ -143,7 +151,7 @@ function createAudioResource(stream, type, additionalPipes = [], ffmpegState = f
], ffmpegState)
}

if (type === 'mp3' && lame) {
if (type === 'mp3' && lame && SampleRate) {
return new NodeLinkStream(stream, [
new lame.Decoder(),
new SampleRate.SampleRate({
Expand Down

0 comments on commit d8da381

Please sign in to comment.