removed unnecessary parts of lib 'speaker'

This commit is contained in:
Daniel Sommer 2022-04-27 16:10:01 +02:00
parent 203e0bcb83
commit 1b64652f21
8 changed files with 7 additions and 243 deletions

3
.gitignore vendored
View file

@ -1,3 +1,4 @@
config.json config.json
node_modules/ node_modules/
npm-debug.log npm-debug.log
libs/speaker/build/

View file

@ -329,7 +329,7 @@ endif
quiet_cmd_regen_makefile = ACTION Regenerating $@ quiet_cmd_regen_makefile = ACTION Regenerating $@
cmd_regen_makefile = cd $(srcdir); /home/velvettear/.nvm/versions/node/v18.0.0/lib/node_modules/npm/node_modules/node-gyp/gyp/gyp_main.py -fmake --ignore-environment "-Dlibrary=shared_library" "-Dvisibility=default" "-Dnode_root_dir=/home/velvettear/.cache/node-gyp/18.0.0" "-Dnode_gyp_dir=/home/velvettear/.nvm/versions/node/v18.0.0/lib/node_modules/npm/node_modules/node-gyp" "-Dnode_lib_file=/home/velvettear/.cache/node-gyp/18.0.0/<(target_arch)/node.lib" "-Dmodule_root_dir=/home/velvettear/development/nodejs/kannon-client/libs/speaker" "-Dnode_engine=v8" "--depth=." "-Goutput_dir=." "--generator-output=build" -I/home/velvettear/development/nodejs/kannon-client/libs/speaker/build/config.gypi -I/home/velvettear/.nvm/versions/node/v18.0.0/lib/node_modules/npm/node_modules/node-gyp/addon.gypi -I/home/velvettear/.cache/node-gyp/18.0.0/include/node/common.gypi "--toplevel-dir=." binding.gyp cmd_regen_makefile = cd $(srcdir); /home/velvettear/.nvm/versions/node/v18.0.0/lib/node_modules/npm/node_modules/node-gyp/gyp/gyp_main.py -fmake --ignore-environment "-Dlibrary=shared_library" "-Dvisibility=default" "-Dnode_root_dir=/home/velvettear/.cache/node-gyp/18.0.0" "-Dnode_gyp_dir=/home/velvettear/.nvm/versions/node/v18.0.0/lib/node_modules/npm/node_modules/node-gyp" "-Dnode_lib_file=/home/velvettear/.cache/node-gyp/18.0.0/<(target_arch)/node.lib" "-Dmodule_root_dir=/home/velvettear/development/nodejs/kannon-client/libs/speaker" "-Dnode_engine=v8" "--depth=." "-Goutput_dir=." "--generator-output=build" -I/home/velvettear/development/nodejs/kannon-client/libs/speaker/build/config.gypi -I/home/velvettear/.nvm/versions/node/v18.0.0/lib/node_modules/npm/node_modules/node-gyp/addon.gypi -I/home/velvettear/.cache/node-gyp/18.0.0/include/node/common.gypi "--toplevel-dir=." binding.gyp
Makefile: $(srcdir)/binding.gyp $(srcdir)/build/config.gypi $(srcdir)/../../../../../.cache/node-gyp/18.0.0/include/node/common.gypi $(srcdir)/deps/mpg123/mpg123.gyp $(srcdir)/../../../../../.nvm/versions/node/v18.0.0/lib/node_modules/npm/node_modules/node-gyp/addon.gypi Makefile: $(srcdir)/deps/mpg123/mpg123.gyp $(srcdir)/binding.gyp $(srcdir)/build/config.gypi $(srcdir)/../../../../../.cache/node-gyp/18.0.0/include/node/common.gypi $(srcdir)/../../../../../.nvm/versions/node/v18.0.0/lib/node_modules/npm/node_modules/node-gyp/addon.gypi
$(call do_cmd,regen_makefile) $(call do_cmd,regen_makefile)
# "all" is a concatenation of the "all" targets from all the included # "all" is a concatenation of the "all" targets from all the included

View file

@ -1,60 +0,0 @@
'use strict'
/**
* Code adapted from:
* https://web.archive.org/web/20110816002016/http://blogs.msdn.com/b/dawate/archive/2009/06/24/intro-to-audio-programming-part-3-synthesizing-simple-wave-audio-using-c.aspx
*/
const Readable = require('stream').Readable
const bufferAlloc = require('buffer-alloc')
const Speaker = require('../')
// the frequency to play
const freq = parseFloat(process.argv[2], 10) || 440.0 // Concert A, default tone
// seconds worth of audio data to generate before emitting "end"
const duration = parseFloat(process.argv[3], 10) || 2.0
console.log('generating a %dhz sine wave for %d seconds', freq, duration)
// A SineWaveGenerator readable stream
const sine = new Readable()
sine.bitDepth = 16
sine.channels = 2
sine.sampleRate = 44100
sine.samplesGenerated = 0
sine._read = read
// create a SineWaveGenerator instance and pipe it to the speaker
sine.pipe(new Speaker())
// the Readable "_read()" callback function
function read (n) {
const sampleSize = this.bitDepth / 8
const blockAlign = sampleSize * this.channels
const numSamples = n / blockAlign | 0
const buf = bufferAlloc(numSamples * blockAlign)
const amplitude = 32760 // Max amplitude for 16-bit audio
// the "angle" used in the function, adjusted for the number of
// channels and sample rate. This value is like the period of the wave.
const t = (Math.PI * 2 * freq) / this.sampleRate
for (let i = 0; i < numSamples; i++) {
// fill with a simple sine wave at max amplitude
for (let channel = 0; channel < this.channels; channel++) {
const s = this.samplesGenerated + i
const val = Math.round(amplitude * Math.sin(t * s)) // sine wave
const offset = (i * sampleSize * this.channels) + (channel * sampleSize)
buf[`writeInt${this.bitDepth}LE`](val, offset)
}
}
this.push(buf)
this.samplesGenerated += numSamples
if (this.samplesGenerated >= this.sampleRate * duration) {
// after generating "duration" second of audio, emit "end"
this.push(null)
}
}

View file

@ -1,10 +0,0 @@
'use strict'
/**
* Pipe data to stdin and it will be played through your speakers.
*/
const Speaker = require('../')
const speaker = new Speaker()
process.stdin.pipe(speaker)

View file

@ -10,7 +10,6 @@
"license": "(MIT AND LGPL-2.1-only)", "license": "(MIT AND LGPL-2.1-only)",
"dependencies": { "dependencies": {
"bindings": "^1.3.0", "bindings": "^1.3.0",
"buffer-alloc": "^1.1.0",
"debug": "^4.0.0" "debug": "^4.0.0"
}, },
"devDependencies": { "devDependencies": {
@ -224,25 +223,6 @@
"integrity": "sha512-qhAVI1+Av2X7qelOfAIYwXONood6XlZE/fXaBSmW/T5SzLAmCgzi+eiWE7fUvbHaeNBQH13UftjpXxsfLkMpgw==", "integrity": "sha512-qhAVI1+Av2X7qelOfAIYwXONood6XlZE/fXaBSmW/T5SzLAmCgzi+eiWE7fUvbHaeNBQH13UftjpXxsfLkMpgw==",
"dev": true "dev": true
}, },
"node_modules/buffer-alloc": {
"version": "1.2.0",
"resolved": "https://registry.npmjs.org/buffer-alloc/-/buffer-alloc-1.2.0.tgz",
"integrity": "sha512-CFsHQgjtW1UChdXgbyJGtnm+O/uLQeZdtbDo8mfUgYXCHSM1wgrVxXm6bSyrUuErEb+4sYVGCzASBRot7zyrow==",
"dependencies": {
"buffer-alloc-unsafe": "^1.1.0",
"buffer-fill": "^1.0.0"
}
},
"node_modules/buffer-alloc-unsafe": {
"version": "1.1.0",
"resolved": "https://registry.npmjs.org/buffer-alloc-unsafe/-/buffer-alloc-unsafe-1.1.0.tgz",
"integrity": "sha512-TEM2iMIEQdJ2yjPJoSIsldnleVaAk1oW3DBVUykyOLsEsFmEc9kn+SFFPz+gl54KQNxlDnAwCXosOS9Okx2xAg=="
},
"node_modules/buffer-fill": {
"version": "1.0.0",
"resolved": "https://registry.npmjs.org/buffer-fill/-/buffer-fill-1.0.0.tgz",
"integrity": "sha1-+PeLdniYiO858gXNY39o5wISKyw="
},
"node_modules/call-bind": { "node_modules/call-bind": {
"version": "1.0.2", "version": "1.0.2",
"resolved": "https://registry.npmjs.org/call-bind/-/call-bind-1.0.2.tgz", "resolved": "https://registry.npmjs.org/call-bind/-/call-bind-1.0.2.tgz",
@ -3546,25 +3526,6 @@
"integrity": "sha512-qhAVI1+Av2X7qelOfAIYwXONood6XlZE/fXaBSmW/T5SzLAmCgzi+eiWE7fUvbHaeNBQH13UftjpXxsfLkMpgw==", "integrity": "sha512-qhAVI1+Av2X7qelOfAIYwXONood6XlZE/fXaBSmW/T5SzLAmCgzi+eiWE7fUvbHaeNBQH13UftjpXxsfLkMpgw==",
"dev": true "dev": true
}, },
"buffer-alloc": {
"version": "1.2.0",
"resolved": "https://registry.npmjs.org/buffer-alloc/-/buffer-alloc-1.2.0.tgz",
"integrity": "sha512-CFsHQgjtW1UChdXgbyJGtnm+O/uLQeZdtbDo8mfUgYXCHSM1wgrVxXm6bSyrUuErEb+4sYVGCzASBRot7zyrow==",
"requires": {
"buffer-alloc-unsafe": "^1.1.0",
"buffer-fill": "^1.0.0"
}
},
"buffer-alloc-unsafe": {
"version": "1.1.0",
"resolved": "https://registry.npmjs.org/buffer-alloc-unsafe/-/buffer-alloc-unsafe-1.1.0.tgz",
"integrity": "sha512-TEM2iMIEQdJ2yjPJoSIsldnleVaAk1oW3DBVUykyOLsEsFmEc9kn+SFFPz+gl54KQNxlDnAwCXosOS9Okx2xAg=="
},
"buffer-fill": {
"version": "1.0.0",
"resolved": "https://registry.npmjs.org/buffer-fill/-/buffer-fill-1.0.0.tgz",
"integrity": "sha1-+PeLdniYiO858gXNY39o5wISKyw="
},
"call-bind": { "call-bind": {
"version": "1.0.2", "version": "1.0.2",
"resolved": "https://registry.npmjs.org/call-bind/-/call-bind-1.0.2.tgz", "resolved": "https://registry.npmjs.org/call-bind/-/call-bind-1.0.2.tgz",

View file

@ -1,131 +0,0 @@
/* eslint-env mocha */
'use strict'
/**
* Module dependencies.
*/
const os = require('os')
const assert = require('assert')
const Speaker = require('../')
const endianness = os.endianness()
const opposite = endianness === 'LE' ? 'BE' : 'LE'
describe('exports', function () {
it('should export a Function', function () {
assert.strictEqual('function', typeof Speaker)
})
it('should have an "api_version" property', function () {
assert(Object.prototype.hasOwnProperty.call(Speaker, 'api_version'))
assert('number', typeof Speaker.api_version)
})
it('should have a "description" property', function () {
assert(Object.prototype.hasOwnProperty.call(Speaker, 'description'))
assert('string', typeof Speaker.description)
})
it('should have a "module_name" property', function () {
assert(Object.prototype.hasOwnProperty.call(Speaker, 'module_name'))
assert('string', typeof Speaker.module_name)
})
})
describe('Speaker', function () {
it('should return a Speaker instance', function () {
const s = new Speaker()
assert(s instanceof Speaker)
})
it('should be a writable stream', function () {
const s = new Speaker()
assert.strictEqual(s.writable, true)
assert.notStrictEqual(s.readable, true)
})
it('should emit an "open" event after the first write()', function (done) {
const s = new Speaker()
let called = false
s.on('open', function () {
called = true
done()
})
assert.strictEqual(called, false)
s.write(Buffer.alloc(0))
})
it('should emit a "flush" event after end()', function (done) {
const s = new Speaker()
let called = false
s.on('flush', function () {
called = true
done()
})
assert.strictEqual(called, false)
s.end(Buffer.alloc(0))
})
it('should emit a "close" event after end()', function (done) {
this.slow(1000)
const s = new Speaker()
let called = false
s.on('close', function () {
called = true
done()
})
assert.strictEqual(called, false)
s.end(Buffer.alloc(0))
})
it('should only emit one "close" event', function (done) {
const s = new Speaker()
let count = 0
s.on('close', function () {
count++
})
assert.strictEqual(0, count)
s.close()
assert.strictEqual(1, count)
s.close()
assert.strictEqual(1, count)
done()
})
it('should accept a device option', function (done) {
const s = new Speaker({ device: 'test' })
assert.strictEqual(s.device, 'test')
s.on('close', done)
s.end(Buffer.alloc(0))
})
it('should not throw an Error if native "endianness" is specified', function () {
assert.doesNotThrow(function () {
// eslint-disable-next-line no-new
new Speaker({ endianness: endianness })
})
})
it('should throw an Error if non-native "endianness" is specified', function () {
assert.throws(function () {
// eslint-disable-next-line no-new
new Speaker({ endianness: opposite })
})
})
it('should throw an Error if a non-supported "format" is specified', function (done) {
const speaker = new Speaker({
bitDepth: 31,
signed: true
})
speaker.once('error', (err) => {
assert.strictEqual('invalid PCM format specified', err.message)
done()
})
speaker.write('a')
})
})

1
package-lock.json generated
View file

@ -7,6 +7,7 @@
"": { "": {
"name": "kannon-client", "name": "kannon-client",
"version": "0.0.1", "version": "0.0.1",
"hasInstallScript": true,
"license": "MIT", "license": "MIT",
"dependencies": { "dependencies": {
"moment": "^2.29.1" "moment": "^2.29.1"

View file

@ -3,7 +3,9 @@
"version": "0.0.1", "version": "0.0.1",
"description": "a multi room audio player", "description": "a multi room audio player",
"main": "kannon-client.js", "main": "kannon-client.js",
"scripts": {}, "scripts": {
"postinstall": "(cd libs/speaker && npm install);"
},
"keywords": [ "keywords": [
"audio", "audio",
"player", "player",