audio-vs1053b
Advanced tools
Comparing version 0.1.5 to 0.1.6
@@ -11,3 +11,3 @@ // Any copyright is dedicated to the Public Domain. | ||
var fs = require('fs'); | ||
var audio = require('audio-vs1053b').use(tessel.port['A']); // Replace '../' with 'audio-vs1053b' in your own code | ||
var audio = require('../').use(tessel.port['A']); // Replace '../' with 'audio-vs1053b' in your own code | ||
@@ -17,3 +17,3 @@ audio.on('ready', function () { // Start recording data for a second into a file | ||
// Open a file | ||
var audioFile = fs.readFileSync('Oops... I did it again.mp3'); | ||
var audioFile = fs.readFileSync('sample.mp3'); | ||
// Play the file | ||
@@ -20,0 +20,0 @@ audio.play(audioFile); |
@@ -5,38 +5,56 @@ // Any copyright is dedicated to the Public Domain. | ||
/********************************************* | ||
This Audio Module demo sets volume, then plays | ||
an audio file out over Headphones/Line out | ||
This Audio Module demo will record audio through | ||
the mic while the CONFIG button is held down. | ||
When the CONFIG button is released, the recorded | ||
audio will be played back through the audio | ||
out jack. | ||
*********************************************/ | ||
var tessel = require('tessel'); | ||
var fs = require('fs'); | ||
var audio = require('../').use(tessel.port['A']); // Replace '../' with 'audio-vs1053b' in your own code | ||
var audioFile = 'sample.mp3'; | ||
var chunks = []; | ||
// When we get data, push it into our array | ||
audio.on('data', function(data) { | ||
chunks.push(data); | ||
}); | ||
// Wait for the module to connect | ||
audio.on('ready', function() { | ||
console.log("Audio module connected! Setting volume..."); | ||
// Set the volume in decibels. Around 20 is good; smaller is louder. | ||
audio.setVolume(20, function(err) { | ||
if (err) { | ||
return console.log(err); | ||
} | ||
// Get the song | ||
console.log('Retrieving file...'); | ||
var song = fs.readFileSync(audioFile); | ||
// Play the song | ||
console.log('Playing ' + audioFile + '...'); | ||
audio.play(song, function(err) { | ||
if (err) { | ||
console.log(err); | ||
} else { | ||
console.log('Done playing', audioFile); | ||
} | ||
console.log('Hold the config button to record...'); | ||
// When the config button is pressed, start recording | ||
tessel.button.once('press', startRecording); | ||
}); | ||
function startRecording() { | ||
// Tell the audio module to start recording | ||
audio.startRecording('voice', function() { | ||
console.log('Recording...'); | ||
// Once the button is released, stop recording | ||
tessel.button.once('release', stopRecording); | ||
}); | ||
} | ||
function stopRecording() { | ||
// Tell the audio module to stop recording | ||
console.log('stopping the recording...'); | ||
audio.stopRecording(function() { | ||
console.log('Playing it back...'); | ||
// Concat the data and play it | ||
audio.play(Buffer.concat(chunks), function(err) { | ||
// When we're done playing, clear recordings | ||
chunks = []; | ||
console.log('Hold the config button to record...'); | ||
// Wait for a button press again | ||
tessel.button.once('press', startRecording); | ||
}); | ||
}); | ||
}); | ||
} | ||
// If there is an error, report it | ||
audio.on('error', function(err) { | ||
console.log(err); | ||
throw err; | ||
}); |
@@ -11,3 +11,3 @@ // Any copyright is dedicated to the Public Domain. | ||
var fs = require('fs'); | ||
var audio = require('audio-vs1053b').use(tessel.port['A']); // Replace '../' with 'audio-vs1053b' in your own code | ||
var audio = require('../').use(tessel.port['A']); // Replace '../' with 'audio-vs1053b' in your own code | ||
@@ -18,4 +18,4 @@ audio.on('ready', function () { | ||
// Open a file | ||
fs.createReadStream('rayman.ogg').pipe(audio.createPlayStream()); | ||
fs.createReadStream('sample.mp3').pipe(audio.createPlayStream()); | ||
}); | ||
}); |
@@ -11,7 +11,7 @@ // Any copyright is dedicated to the Public Domain. | ||
var fs = require('fs'); | ||
var audio = require('audio-vs1053b').use(tessel.port['A']); // Replace '../' with 'audio-vs1053b' in your own code | ||
var audio = require('../').use(tessel.port['A']); // Replace '../' with 'audio-vs1053b' in your own code | ||
audio.on('ready', function () { | ||
// Start recording data for a second into a file | ||
audio.setInput('line-in', function(err) { | ||
audio.setInput('lineIn', function(err) { | ||
// Open a stream to a file | ||
@@ -23,3 +23,5 @@ var file = fs.createWriteStream('lineInData.ogg'); | ||
soundData.pipe(file); | ||
// Stop recording after 2 seconds | ||
setTimeout(audio.stopRecording.bind(audio), 2000); | ||
}); | ||
}); |
11
index.js
@@ -180,3 +180,3 @@ // Copyright 2014 Technical Machine, Inc. See the COPYRIGHT | ||
// Check if this chunk is too small to be played solo | ||
if (this.bufferedLen >= 5000) { | ||
if (this.bufferedLen >= 10000) { | ||
var audioData = Buffer.concat(this.bufs); | ||
@@ -217,11 +217,6 @@ this.bufs = []; this.bufferedLen = 0; | ||
audio.on('data', function(data) { | ||
bufQueue = bufQueue.concat(data); | ||
recordStream.push(data); | ||
}); | ||
recordStream._read = function(size) { | ||
var toSend = size > bufQueue.length ? bufQueue.length : size; | ||
for (var i = 0; i < toSend; i++) { | ||
recordStream.push(bufQueue[i]); | ||
} | ||
} | ||
recordStream._read = function(size) {} | ||
@@ -228,0 +223,0 @@ process.once('audio_recording_complete', function() { |
{ | ||
"name": "audio-vs1053b", | ||
"version": "0.1.5", | ||
"version": "0.1.6", | ||
"description": "Library to run the Tessel Audio Module. Plays and records sound.", | ||
@@ -5,0 +5,0 @@ "main": "index.js", |
145
README.md
#Audio | ||
Driver for the audio-vs1053b Tessel audio module ([VS1053B](https://www.google.com/url?sa=t&rct=j&q=&esrc=s&source=web&cd=2&cad=rja&uact=8&ved=0CC8QFjAB&url=http%3A%2F%2Fwww.vlsi.fi%2Ffileadmin%2Fdatasheets%2Fvlsi%2Fvs1053.pdf&ei=YXiFU4LrE8P3oATVwICADA&usg=AFQjCNF4G61PCB76XncJH2V8x9NE18ycqg&bvm=bv.67720277,d.cGU)) | ||
Driver for the audio-vs1053b Tessel audio module. The hardware documentation for this module can be found [here](https://github.com/tessel/hardware/blob/master/modules-overview.md#audio). | ||
##Installation | ||
If you run into any issues you can ask for support on the [Audio Module Forums](http://forums.tessel.io/category/audio). | ||
###Installation | ||
```sh | ||
@@ -9,3 +11,3 @@ npm install audio-vs1053b | ||
##Limitations | ||
###Limitations | ||
The current version of the Tessel runtime is too slow to play audio files smoothly. That means we wrote a custom C shim that handles most of the playback and recording of data. There are several consequences of the C shim: | ||
@@ -19,8 +21,7 @@ | ||
###Development Status | ||
Playback and recording to/from the local file system works well. Streams work less well. Interacting with the SDCard, Ambient, and IR doesn't work yet (issues with the SPI bus). This module is currently undergoing heavy development to fix those issues. Please file any bugs you find with this module. | ||
##Example | ||
```.js | ||
// Any copyright is dedicated to the Public Domain. | ||
// http://creativecommons.org/publicdomain/zero/1.0/ | ||
###Example | ||
```js | ||
/********************************************* | ||
@@ -33,17 +34,25 @@ This Audio Module demo sets volume, then plays | ||
var fs = require('fs'); | ||
var audio = require('../').use(tessel.port('a')); | ||
var audio = require('audio-vs1053b').use(tessel.port['A']); | ||
console.log('trying to connect...'); | ||
var audioFile = 'sample.mp3'; | ||
// Wait for the module to connect | ||
audio.on('ready', function() { | ||
console.log("Ready to go!"); | ||
audio.setVolume(20, 20, function(err) { | ||
if (err) return console.log('err setting volume', err); | ||
var song = fs.readFileSync('/app/sample.mp3'); | ||
console.log("Audio module connected! Setting volume..."); | ||
// Set the volume in decibels. Around 20 is good; smaller is louder. | ||
audio.setVolume(20, function(err) { | ||
if (err) { | ||
return console.log(err); | ||
} | ||
// Get the song | ||
console.log('Retrieving file...'); | ||
var song = fs.readFileSync(audioFile); | ||
// Play the song | ||
console.log('Playing ' + audioFile + '...'); | ||
audio.play(song, function(err) { | ||
if (err) { | ||
console.log("error playing song: ", err); | ||
console.log(err); | ||
} else { | ||
console.log('Done playing', audioFile); | ||
} | ||
else { | ||
console.log("Done playing the first song"); | ||
} | ||
}); | ||
@@ -53,74 +62,88 @@ }); | ||
// If there is an error, report it | ||
audio.on('error', function(err) { | ||
console.log("Failed to connect", err); | ||
console.log(err); | ||
}); | ||
``` | ||
##Methods | ||
###Methods | ||
##### * `audio.setVolume(leftChannelDb, [rightChannelDb,] callback(err))` Set the output volume. Level is a Number from 0.0 to 1.0 | ||
 <a href="#api-audio-setVolume-leftChannelDb-rightChannelDb-callback-err-Set-the-output-volume-Level-is-a-Number-from-0-0-to-1-0" name="api-audio-setVolume-leftChannelDb-rightChannelDb-callback-err-Set-the-output-volume-Level-is-a-Number-from-0-0-to-1-0">#</a> audio<b>.setVolume</b>( leftChannelDb, [rightChannelDb,] callback(err) ) | ||
Set the output volume. Level is a Number from 0.0 to 1.0 | ||
##### * `audio.setInput(input, callback(err))` Set the input to either 'lineIn' or 'mic'. Defaults to 'lineIn'. | ||
 <a href="#api-audio-setInput-input-callback-err-Set-the-input-to-either-lineIn-or-mic-Defaults-to-lineIn" name="api-audio-setInput-input-callback-err-Set-the-input-to-either-lineIn-or-mic-Defaults-to-lineIn">#</a> audio<b>.setInput</b>( input, callback(err) ) | ||
Set the input to either 'lineIn' or 'mic'. Defaults to 'lineIn'. | ||
##### * `audio.setOutput(output, callback(err))` Set the output to either 'lineOut' or 'headPhones'. Defaults to 'lineOut'. | ||
 <a href="#api-audio-setOutput-output-callback-err-Set-the-output-to-either-lineOut-or-headPhones-Defaults-to-lineOut" name="api-audio-setOutput-output-callback-err-Set-the-output-to-either-lineOut-or-headPhones-Defaults-to-lineOut">#</a> audio<b>.setOutput</b>( output, callback(err) ) | ||
Set the output to either 'lineOut' or 'headPhones'. Defaults to 'lineOut'. | ||
##### * `audio.startRecording([profile] callback(err))` Start recording sound from the input. (Receive data in the 'data' event) Callback called after recording initialized (not stopped). quality is an optional argument that can be 'voice', 'wideband-voice', 'wideband-stereo', 'hifi-voice', or 'stereo-music'. Default is 'hifi-voice'. | ||
 <a href="#api-audio-startRecording-profile-callback-err-Start-recording-sound-from-the-input-Receive-data-in-the-data-event-Callback-called-after-recording-initialized-not-stopped-quality-is-an-optional-argument-that-can-be-voice-wideband-voice-wideband-stereo-hifi-voice-or-stereo-music-Default-is-hifi-voice" name="api-audio-startRecording-profile-callback-err-Start-recording-sound-from-the-input-Receive-data-in-the-data-event-Callback-called-after-recording-initialized-not-stopped-quality-is-an-optional-argument-that-can-be-voice-wideband-voice-wideband-stereo-hifi-voice-or-stereo-music-Default-is-hifi-voice">#</a> audio<b>.startRecording</b>( [profile] callback(err) ) | ||
Start recording sound from the input. (Receive data in the 'data' event) Callback called after recording initialized (not stopped ) .quality is an optional argument that can be 'voice', 'wideband-voice', 'wideband-stereo', 'hifi-voice', or 'stereo-music'. Default is 'hifi-voice'. | ||
##### * `audio.stopRecording(callback(err))` Stop recording sound (note that may receive one more 'data' event before this completes when the buffer is flushed.) | ||
 <a href="#api-audio-stopRecording-callback-err-Stop-recording-sound-note-that-may-receive-one-more-data-event-before-this-completes-when-the-buffer-is-flushed" name="api-audio-stopRecording-callback-err-Stop-recording-sound-note-that-may-receive-one-more-data-event-before-this-completes-when-the-buffer-is-flushed">#</a> audio<b>.stopRecording</b>( callback(err) ) | ||
Stop recording sound (note that may receive one more 'data' event before this completes when the buffer is flushed. ) | ||
##### * `audio.play([audioBuff], callback(err))` Play a buffer. If no buffer is passed in, the module will attempt to resume a buffer that was paused. | ||
 <a href="#api-audio-play-audioBuff-callback-err-Play-a-buffer-If-no-buffer-is-passed-in-the-module-will-attempt-to-resume-a-buffer-that-was-paused" name="api-audio-play-audioBuff-callback-err-Play-a-buffer-If-no-buffer-is-passed-in-the-module-will-attempt-to-resume-a-buffer-that-was-paused">#</a> audio<b>.play</b>( [audioBuff], callback(err) ) | ||
Play a buffer. If no buffer is passed in, the module will attempt to resume a buffer that was paused. | ||
##### * `audio.pause(callback(err))` Pause the buffer | ||
 <a href="#api-audio-pause-callback-err-Pause-the-buffer" name="api-audio-pause-callback-err-Pause-the-buffer">#</a> audio<b>.pause</b>( callback(err) ) | ||
Pause the buffer. | ||
##### * `audio.stop(callback(err))` Stop playing and flush the buffer | ||
 <a href="#api-audio-stop-callback-err-Stop-playing-and-flush-the-buffer" name="api-audio-stop-callback-err-Stop-playing-and-flush-the-buffer">#</a> audio<b>.stop</b>( callback(err) ) | ||
Stop playing and flush the buffer. | ||
##### * `audio.createPlayStream()` Returns a stream that a buffer can be piped into to play audio | ||
 <a href="#api-audio-createPlayStream-Returns-a-stream-that-a-buffer-can-be-piped-into-to-play-audio" name="api-audio-createPlayStream-Returns-a-stream-that-a-buffer-can-be-piped-into-to-play-audio">#</a> audio<b>.createPlayStream</b>() | ||
Returns a stream that a buffer can be piped into to play audio. | ||
##### * `audio.createRecordStream()` Returns a readable stream of mic data | ||
 <a href="#api-audio-createRecordStream-Returns-a-readable-stream-of-mic-data" name="api-audio-createRecordStream-Returns-a-readable-stream-of-mic-data">#</a> audio<b>.createRecordStream</b>() | ||
Returns a readable stream of mic data. | ||
##### * `audio.availableRecordingProfiles()` Returns an array of available profiles | ||
 <a href="#api-audio-availableRecordingProfiles-Returns-an-array-of-available-profiles" name="api-audio-availableRecordingProfiles-Returns-an-array-of-available-profiles">#</a> audio<b>.availableRecordingProfiles</b>() | ||
Returns an array of available profiles. | ||
###Events | ||
##Events | ||
 <a href="#api-audio-on-ready-callback-The-audio-module-is-ready-to-use" name="api-audio-on-ready-callback-The-audio-module-is-ready-to-use">#</a> audio<b>.on</b>( 'ready', callback() ) | ||
The audio module is ready to use. | ||
##### * `audio.on('ready', callback())` The audio module is ready to use | ||
 <a href="#api-audio-on-error-callback-err-The-audio-module-had-an-error-on-connection" name="api-audio-on-error-callback-err-The-audio-module-had-an-error-on-connection">#</a> audio<b>.on</b>( 'error', callback(err) ) | ||
The audio module had an error on connection. | ||
##### * `audio.on('error', callback(err))` The audio module had an error on connection | ||
 <a href="#api-audio-on-volume-callback-volume-Volume-was-set" name="api-audio-on-volume-callback-volume-Volume-was-set">#</a> audio<b>.on</b>( 'volume', callback(volume) ) | ||
Volume was set. | ||
##### * `audio.on('volume', callback(volume))` Volume was set | ||
 <a href="#api-audio-on-input-callback-input-The-input-mode-was-set" name="api-audio-on-input-callback-input-The-input-mode-was-set">#</a> audio<b>.on</b>( 'input', callback(input) ) | ||
The input mode was set. | ||
##### * `audio.on('input', callback(input))` The input mode was set | ||
 <a href="#api-audio-on-output-callback-output-The-output-mode-was-set" name="api-audio-on-output-callback-output-The-output-mode-was-set">#</a> audio<b>.on</b>( 'output', callback(output) ) | ||
The output mode was set. | ||
##### * `audio.on('output', callback(output))` The output mode was set | ||
 <a href="#api-audio-on-startRecording-callback-Started-recording-from-the-input" name="api-audio-on-startRecording-callback-Started-recording-from-the-input">#</a> audio<b>.on</b>( 'startRecording', callback() ) | ||
Started recording from the input. | ||
##### * `audio.on('startRecording', callback())` Started recording from the input | ||
 <a href="#api-audio-on-data-callback-audioBuff-Received-recorded-data" name="api-audio-on-data-callback-audioBuff-Received-recorded-data">#</a> audio<b>.on</b>( 'data', callback(audioBuff) ) | ||
Received recorded data. | ||
##### * `audio.on('data', callback(audioBuff))` Received recorded data | ||
 <a href="#api-audio-on-stopRecording-callback-Stopped-recording-on-the-input" name="api-audio-on-stopRecording-callback-Stopped-recording-on-the-input">#</a> audio<b>.on</b>( 'stopRecording', callback() ) | ||
Stopped recording on the input. | ||
##### * `audio.on('stopRecording', callback())` Stopped recording on the input | ||
 <a href="#api-audio-on-play-callback-A-buffer-is-beginning-to-be-played" name="api-audio-on-play-callback-A-buffer-is-beginning-to-be-played">#</a> audio<b>.on</b>( play', callback() ) | ||
A buffer is beginning to be played. | ||
##### * `audio.on('play', callback())` A buffer is beginning to be played | ||
 <a href="#api-audio-on-pause-callback-Playback-was-paused" name="api-audio-on-pause-callback-Playback-was-paused">#</a> audio<b>.on</b>( 'pause', callback() ) | ||
Playback was paused. | ||
##### * `audio.on('pause', callback())` Playback was paused | ||
 <a href="#api-audio-on-stop-callback-Playback-was-stopped" name="api-audio-on-stop-callback-Playback-was-stopped">#</a> audio<b>.on</b>( 'stop', callback() ) | ||
Playback was stopped. | ||
##### * `audio.on('stop', callback())` Playback was stopped | ||
 <a href="#api-audio-on-end-callback-err-The-buffer-finished-playing" name="api-audio-on-end-callback-err-The-buffer-finished-playing">#</a> audio<b>.on</b>( 'end', callback(err) ) | ||
The buffer finished playing. | ||
##### * `audio.on('end', callback(err)` The buffer finished playing | ||
###Further Examples | ||
* [Audio Out No Streams](https://github.com/tessel/audio-vs1053b/blob/master/examples/audio-out-no-streams.js). This Audio Module demo sends audio from a file to Headphones/Line out without using streams.. | ||
* [Record Sound](https://github.com/tessel/audio-vs1053b/blob/master/examples/record-sound.js). This Audio Module demo sends audio from the microphone to a file without using streams. | ||
* [Stream Audio Out](https://github.com/tessel/audio-vs1053b/blob/master/examples/stream-audio-out.js). This Audio Module demo sends audio from a file to Headphones/Line out using streams. | ||
* [Stream Sound to File](https://github.com/tessel/audio-vs1053b/blob/master/examples/stream-sound-to-file.js). This Audio Module demo takes line-in and writes it to a file using streams. | ||
##Further Examples | ||
See the examples folder for code. | ||
* audio-out-no-streams: Listen to audio without using streams. | ||
* record-sound: Record sound from the microphone without using streams. | ||
* stream-audio-out: Stream audio from a file to Headphones/Line out | ||
* stream-sound-to-file: Stream audio input from line in to a file. | ||
##License | ||
MIT | ||
APACHE | ||
###License | ||
MIT or Apache 2.0, at your option |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
Network access
Supply chain riskThis module accesses the network.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
Native code
Supply chain riskContains native code (e.g., compiled binaries or shared libraries). Including native code can obscure malicious behavior.
Found 10 instances in 1 package
1005
146
2
427635
21