Sound detector is not working with Johnny-five - javascript

I have two sound sensors which both are Arduino compatible. One is AVR PIC F Arduino Sensitive Audio Sound Microphone Mic Sensor Detection Module, another is just a different brand comes with Arduino kit. I tried to use the following code with Johnny-five:
var five = require("johnny-five");
var board = new five.Board();
board.on("ready", function() {
var mic = new five.Sensor("A0");
mic.on("data", function() {
console.log(this.value);
});
});
I connected Digital output to A1 and Analog output to A0, and I tried only connecting to A0 without digital too. However, the AO output which is the (this.value in the code) are appearing a bit fluctuating but mainly steady value that is not affected by sound input at all.
I tried C code to test the sensor, it works. So the hardware is good but I am sure the connection is correct or I need any extra code for this?
Anyone has a sound sensor working with Johnny-five, please advise.
Thank you so much!

Related

Read i2c sensor with johnny-five library (Adafruit_AS726x)

I'm writing code with the johnny five library to pilote a process. Sometime I find some i2c device not include in the johnny-five library. One of them is the Adafruit AS7262 (https://www.adafruit.com/product/3779)
With johnny-five we could read and write i2c like describe here : http://johnny-five.io/api/board/
With :
var five = require("johnny-five");
var board = new five.Board();
board.on("ready", function() {
this.i2cConfig();
this.i2cWrite(0x01, 0x00, [0x02, 0x03]);
});
enter code here
And :
var five = require("johnny-five");
var board = new five.Board();
board.on("ready", function() {
this.i2cConfig();
this.i2cRead(0x01, 0x00, 6, function(bytes) {
console.log("Bytes read: ", bytes);
});
});
In the documentation of the Adafruit AS7262 https://cdn-learn.adafruit.com/assets/assets/000/052/623/original/AS7262_DS000486_2-00_%281%29.pdf?1522179774
or in the code of the library for arduino use : https://github.com/adafruit/Adafruit_AS726x/blob/master/Adafruit_AS726x.h
I find some HEX adress that I have try to use for acess the datas from the device but i get nothing and when i put some light on the device the output value doesn't changes here my code :
this.i2cConfig();
this.i2cRead(0x49,0x0D,16,(byte)=>{
console.log("Bytes read : " + byte)
})
0x49 is suppose to be the adress of the device
0x0D is suppose to be the register adress for the value of intensity of green light
and I read 16 bytes from there if i well understand but i get this :
Bytes read : 0,0,0,114,64,0,0,0,0,0,0,0,0,0,0,0
And nothing change with light intensity changes (it's work when i run the Arduino library, so the device works)
I try to initiate the device with the write method but i dont understand what put in to make it work.
I never use i2c protocol before so I probably don't understand something basic.

How can I make javascript sing?

I saw that there's some singing voices in the Web Speech API using the voice "Good News" (and "Bad News"). But I'd like to know how I can make my own custom song. It seems to sing the same notes. Can I pass it what notes I'd like for each word?
edit: a comment below points out the "Good News" is a mac voice. So the below code might only work on Mac. I'm looking for a more generalized solution for both pc and mac.
if (!window.speechSynthesis) {
alert("Your browser doesn't support speechSynthesis !!");
}
var voices;
function loadVoices() {
voices = window.speechSynthesis.getVoices();
if (voices.length == 0) alert("cannot get voices !!");
console.log(voices.map(function (x) {return x.name;}));
}
var msg = new SpeechSynthesisUtterance("la-la-la-la-la-la-la-la-la-la-la-la-la-la-la-la-la-la-la-la-la-la-la-la-la-la-la");
function sing(name) {
speechSynthesis.cancel();
loadVoices();
msg.voice = voices[0];
for (var i = 0; i < voices.length; i++)
if (voices[i].name == name) msg.voice = voices[i];
speechSynthesis.speak(msg);
}
setTimeout(function() {
sing('Good News');
},1000); // let browser initialize for a sec
I noticed Web Speech api has something called pitch. But after messing with it, the word doesn't really "sing". You can see a codepen here. Also I don't think I could convert a frequency to a pitch, like for example Middle C is 261.6 Hz.
I also noticed another library called meSing.js but the voices sound too robotic:
http://usdivad.com/mesing/
I downloaded mesing.js from github, and I changed the hard coded voice that it uses to other voices that are given in the underlying voice library (meSpeak.js), and even though the voice changes, it still sounds robotic.
I like the sound quality of the first "Good News" but I'd like to know how to make a custom one, is it possible?
edit: maybe I can take 'Good News' with just one word. and then play that with different pitch settings. I'd have to see what the range of pitches are. still experimenting..

VideoJS HTML5 Video JS How to boost volume above maximum?

Its possible there's no solution to this but I thought I'd inquire anyway. Sometimes a video is really quiet and if I turn the volume of my computer up accordingly then other sounds I have become way too loud as a result. It would be nice to be able to boost the volume above maximum.
I did a search on google which literally turned up nothing at all, not even results related to videojs at all in fact. Some videos my Mac is almost at max volume to hear the video's speech well so it would not be feasible to start with everything at a lower volume and adjust accordingly.
I tried with:
var video = document.getElementById("Video1");
video.volume = 1.0;
And setting it to anything above 1.0 but the video then fails to open at all:
var video = document.getElementById("Video1");
video.volume = 1.4; /// 2.0 etc
Based on: http://cwestblog.com/2017/08/17/html5-getting-more-volume-from-the-web-audio-api/
You can adjust the gain by using the Web Audio API:
function amplifyMedia(mediaElem, multiplier) {
var context = new(window.AudioContext || window.webkitAudioContext),
result = {
context: context,
source: context.createMediaElementSource(mediaElem),
gain: context.createGain(),
media: mediaElem,
amplify: function(multiplier) {
result.gain.gain.value = multiplier;
},
getAmpLevel: function() {
return result.gain.gain.value;
}
};
result.source.connect(result.gain);
result.gain.connect(context.destination);
result.amplify(multiplier);
return result;
}
amplifyMedia(document.getElementById('myVideo'), 1.4);
The multiplier you pass to the function is at same level as the video volume, 1 being the 100% volume, but in this case you can pass a higher number.
Can't post any working demo or JSFiddle because Web Audio API requires a source from the same origin (or CORS compatible). You can see the error in the console: https://jsfiddle.net/yuriy636/41vrx1z7/1/
MediaElementAudioSource outputs zeroes due to CORS access restrictions
But I have tested locally and it works as intended.
If you have access to the source files, rather than trying to boost on the fly using Javascript (for that the Web Audio API answer from #yuriy636 is the best solution) then you can process the video locally using something like ffmpeg.
ffmpeg -i input.mp4 -filter:a "volume=1.5" output.mp4
This will apply a filter to the input.mp4 file that just adjusts the volume to 1.5x the input and creates a new file called output.mp4.
You can also set a decibel level:
ffmpeg -i input.mp4 -filter:a "volume=10dB" output.mp4
or review the instructions to normalize audio etc.

Play Real-time Notification Sound to all the users logged in the Webapp Without reloading the page

I know there are many solutions can be found in the web regarding my problem, but none of them are working for me. That's why I'm asking this question.
First let me explain what I'm looking to achieve -
-> I'm developing a multi-user Web application [ASP.Net]
-> I'm using SignalR to get real-time database change notifications and SignalR instantly transmit the change notifications to all the users logged in the application.
-> Now in addition what I want to do is to play a notification sound for all the logged in users so that they can understand a new notification need attention.
This is what I've done so far -
JavaScript
<script>
function playSound(mysound) {
//thisSound = document.getElementById(mysound);
//thisSound.Play();
var audio = new Audio(mysound);
audio.play();
}
</script>
Code Behind -
ScriptManager.RegisterClientScriptBlock(Me, [GetType](), "OpenWindow", "javascript: playSound('./audio/notification.wav')", True)
The problem with this solution is that the user need to reload the page to hear the notification sound, which I think is pointless if the user can't hear them instantly like the SignalR processing notifications.
Is it possible to push the sound to all the clients so that they don't need to reload the page?
Any help would be highly appreciated. If you need any further clarification please let me know.
Finally I got it worked. I had to change the jquery a little bit -
<script type="text/javascript">
function playSound(mysound) {
//thisSound = document.getElementById(mysound);
//thisSound.Play();
var audio = new Audio(mysound);
audio.play();
}
$(function () {
var notify = $.connection.notificationsHub;
var audio;
notify.client.displayNotification = function (s_Not, s_Path) {
$("#newNot").html(s_Not);
audio = new Audio(s_Path);
var iLevel = "<%=System.Web.HttpContext.Current.Session("USER_LEVEL")%>";
var i_OldNot = "<%=System.Web.HttpContext.Current.Session("NOT_COUNT")%>";
if (iLevel == 2) {
//alert(i_OldNot + " " + s_Not);
if (i_OldNot < i_Not) {
playSound("/audio/notification.wav");
//i_OldNot == Number(s_not);
}
}
};
$.connection.hub.start();
});
</script>
In the code behind I had to set a Session Variable to store the number of last notification before update. If the previous and present number of notification is higher than the session value then notification sound play -
System.Web.HttpContext.Current.Session("NOT_COUNT")= i_LastNotCount
Now the sound is playing without reloading the page. Special thanks to rdans, because of his below comments I got this idea -
Then in a comment you say:
I already have implemented SignalR. So it's not the problem at all.
This suggests to me that you probably don't understand what signalR is doing because the whole point of signalR is to push to the browser without having to post back or use an ajax call.
The time you are executing the user logged in notification using signalR, after that you can Call javascript function from server .
Hope this is what you are looking for.

Web audio: Can't get ScriptProcessor node to work in Chrome

In web audio, I can't get the ScriptProcessor node to work in Chrome, although it works fine in Firefox.
// Create audio context (Chrome/Firefox)
var context;
if (window.AudioContext) {
context = new AudioContext();
} else {
context = new webkitAudioContext();
}
// Create oscillator and start it
oscillator = context.createOscillator();
oscillator.start(0);
// Set up a script node that sets output to white noise
var myscriptnode = context.createScriptProcessor(4096, 1, 1);
myscriptnode.onaudioprocess = function(event) {
console.log('Processing buffer');
var output = event.outputBuffer.getChannelData(0);
for (i = 0; i < output.length; i++) {
output[i] = Math.random() / 10;
}
};
// Connect oscillator to script node and script node to destination
// (should output white noise)
oscillator.connect(myscriptnode);
myscriptnode.connect(context.destination);
// NOTE: This commented-out code connects oscillator directly to
// destination, which works in Chrome as well as Firefox.
//oscillator.connect(context.destination);
Expected result of this sample is that it should play white noise at 1/10 volume (the oscillator is actually ignored).
You can try this code at http://jsfiddle.net/78yKV/3/ - be aware that on Firefox this URL will play white noise straight away! On Chrome 30, it doesn't give any errors, but also doesn't give any audio output. I also checked in Chrome 31 beta but saw the same results. The 'Processing buffer' log entry never appears.
To test the general audio system, if you uncomment the last line and connect the oscillator directly to the destination, it does play audio (the oscillator tone) correctly on Chrome. But I can't get the ScriptProcessor to work on Chrome.
I searched the net for tutorials etc. with ScriptProcessor but those I found either didn't come with runnable examples or didn't work (or were too complex).
(Just to make clear - this is a stripped-down sample and doesn't relate in any way to what I'm actually trying to do, so please don't tell me that I shouldn't use a ScriptProcessor to generate white noise. That's not what it's for; I do absolutely need ScriptProcessor to work for my real usage.)
I think most likely I am doing something very stupid like I have the wrong event name or something like that, but I can't find it. Can anyone help?
I now managed to check on several other machines and I think the problem is specific to the default audio device on my machine, which is a telephone handset using the Microsoft default USB audio driver. I've reported this to Google using the menu option in Chrome; my speculation is that the problem occurs because the handset only supports mono 16 kHz output, and somehow this causes Chrome to get confused.
I can reproduce the bug on a colleague's machine which has the same make of handset. To reiterate:
Firefox works correctly on both machines when using the handset.
Both machines work correctly in Chrome when you select a different output device.
The oscillator playback works correctly in Chrome even when using the telephone handset.
Final version of test code http://jsfiddle.net/78yKV/7/
function doStuff(osc) {
// Create audio context (Chrome/Firefox)
var context;
if (window.AudioContext) {
context = new AudioContext();
} else {
context = new webkitAudioContext();
}
// Set up a script node that sets output to white noise
var myscriptnode;
if (context.createScriptProcessor) {
myscriptnode = context.createScriptProcessor(4096, 1, 1);
} else {
myscriptnode = context.createJavaScriptNode(4096, 1, 1);
}
var buffer = 1;
myscriptnode.onaudioprocess = function(event) {
console.log('Processing buffer ' + (buffer++));
var output = event.outputBuffer.getChannelData(0);
for (i = 0; i < output.length; i++) {
output[i] = Math.random() / 10;
}
};
// Connect script node to destination
if (osc) {
oscillator = context.createOscillator();
oscillator.start(0);
oscillator.connect(context.destination);
} else {
myscriptnode.connect(context.destination);
}
}
The white noise playback from this script (well actually a slightly earlier test version but I think it's the same) works in Chrome 30 on Windows 7, Windows 8.1, Linux, and Android 4.1; on Firefox on Windows; on an iPad (latest OS); and on a Mac using Safari 6.0.5 as well (it breaks if you open the developer tools there, but as long as you don't, it works). It only fails when using the USB telephone handset (Polycom CX300) mentioned.
So in other words, as apsillers suggested, this still looks like a Chrome bug, but a rather specific one. (By the way I also tried the latest 'Canary' version of Chrome but it didn't help.)

Categories