Audioworklet loop - web-audio-api

I'm trying to create an audioworklet that loops a single instance of a sound to create a sort of sustain effect. No matter how many sections of the input I loop, I keep hearing a blip type skipping sound. How many calls to the processor is one instance?
To give you an idea, this is what I have so far:
constructor() {
super();
this.sound = [];
this.count = 20;
this.step = [0, 0];
}
process(inputs, outputs, parameters) {
if (inputs && inputs.length) {
for (var i = 0; i < inputs[0].length; i++) {
var input = inputs[0][i];
var output = outputs[0][i];
if (!this.sound[i]) {
this.sound[i] = [];
}
if (this.sound[i].length < this.count) {
this.sound[i].push([]);
for (var j = 0; j < input.length; j++) {
this.sound[i][this.sound[i].length - 1][j] = input[j];
}
} else if (this.sound[i]) {
var s = this.sound[i][this.step[i] % this.sound[i].length];
for (var j = 0; j < s.length; j++) {
output[j] = s[j];
}
this.step[i]++;
}
}
}
return true;
}
So the idea is that I capture the incoming input in an array of N length for each channel(in my case there are 2 channels). Then once that array is full, I cycle through that array to fill the output until the node is disabled.

Thanks for the fiddle. Very helpful.
I didn't look to see what was causing the clicks, but I took your example and modified it very slightly like so:
// #channels 2
// #duration 1.0
// #sampleRate 44100
var bufferSize = 4096;
let ctx = context;
let osc = ctx.createOscillator();
osc.start();
let myPCMProcessingNode = ctx.createScriptProcessor(bufferSize, 2, 2);
let _count = 1;
var sound = [];
var count = _count++;
console.log(count)
var hesitate = 0;
var step = [0, 0];
//Logic to pay attention to
myPCMProcessingNode.onaudioprocess = function(e) {
for(var i = 0; i<2; i++){
var input = e.inputBuffer.getChannelData(i);
var output = e.outputBuffer.getChannelData(i);
if (!sound[i]) {
sound[i] = [];
}
if (sound[i].length < count) {
sound[i].push([]);
for (var j = 0; j < input.length; j++) {
sound[i][sound[i].length - 1][j] = input[j];
}
} else if (sound[i]) {
var s = sound[i][step[i] % sound[i].length];
for (var j = 0; j < s.length; j++) {
output[j] = s[j];
}
step[i]++;
}
}
}
//To hear
osc.connect(myPCMProcessingNode).connect(ctx.destination);
I pasted this in the code window at https://hoch.github.io/canopy. Press the top left button (arrow) to the left of the code window, and you can see the rendered audio. You can see that the output (frequency is 10 instead of 440 to make it easier to see) is discontinuous. This causes the clicks you hear. You can also change the frequency to 100 and find discontinuities in the output.
I hope this is enough to help you figure out what's wrong with your buffering.
An alternative is to create an AudioBufferSourceNode with an AudioBuffer of the basic sample. You can set the AudioBufferSourceNode to loop the whole buffer. This doesn't solve the clicking problem, but it is somewhat simpler.
But this is a general problem of looping any buffer. Unless you arrange the last sample to be close to the first sample, you will hear clicks when you wrap around. You either need to grab chunks where the first and last samples are nearly the same, or modified the chunks so they ramp up at the beginning in some way and ramp down at the end in some way.

Related

flutter serial communication via bluetooth

I am sending some values from a microcontroller to a flutter app in the form of Uint8List then I convert it to. When I send values individually, I have no issue at all. however, when I send 100 values together or any number of values continuously, I get some values correct and others divided in multiple lines. I will be providing a screenshot for the result because its hard to represent it with typing.
This is the code tor receiving:
Future<void> _onDataReceived( Uint8List data ) async {
var fixedList = new List<String>.filled(6, '', growable: true);
// var fixedList1 = <Uint8List>[];
var fixed1 = new List<int>.filled(7, 0, growable: true);
// fixedList1.add(data);
if (data.length >= 7) {
for (int k = 0; k < 7; k++) {
// int x=0;
fixed1[k] = data[k+1];
l++;
}
if (l > 6) {
for (int i = 0; i <= 5; i++) {
fixedList[i] = String.fromCharCode(fixed1[i]);
fixed1[i]=0;
}
l = 0;
fixed1.clear();
print(fixedList); //fixedList1.clear();
}
}
}
Anyone knows what is going on here?

Flutter Random Number generator for different quiz sizes

I'm building a language teaching app that provides the user with a quiz for every topic.
I've found a neat solution for the quiz-structure.
But the problem is, that every quiz has a different size and I don't know how to adjust the Random number generator, so that it'll produce only random numbers for the current quiz-size.
I need a self-adjusting RNG, but I have no imagination of how to do this.
Here's my random number generator.
genrandomarray() {
var distinctIds = [];
var rand = new Random();
for (int i = 0;;) {
distinctIds.add(rand.nextInt(10) + 1);
random_array = distinctIds.toSet().toList();
if (random_array.length < 10) {
continue;
} else {
break;
}
}
print(random_array);
}
For example one topic has 12 questions, one 30, and so on.
Random rnd;
int min = 0;
int max = 10;
rnd = new Random();
r = min + rnd.nextInt(max - min);
print("$r is in the range of $min and $max");
If you can pass by parameter the maximum value is a correct way:
genrandomarray(int max_value) {
var distinctIds = [];
var rand = new Random();
rand = rand.nextInt(max_value);
for (int i = 0;i<rand;i++) {
distinctIds.add(rand.nextInt(10) + 1);
random_array = distinctIds.toSet().toList();
if (random_array.length < 10) {
continue;
} else {
break;
}
}
print(random_array);
}

getEditResponseUrl -> blank form for some rows

I have been using the getEditResponseUrl method for quite some time. However now in the list of over 400 entries some of the urls which are generated end up with a blank form instead of the previously added content. Any idea on solving this?
function linksmaken() {
var urlCol = 64; // kolom nr 64 begint met 1
var responses = form.getResponses();
for (var i = 0; i < responses.length; i++) {
timestamps.push(responses[i].getTimestamp().setMilliseconds(0));
urls.push(responses[i].getEditResponseUrl());
}
for (var j = 1; j < data.length; j++) {
resultUrls.push([data[j][0]?urls[timestamps.indexOf(data[j][0].setMilliseconds(0))]:'']);
}
sheet.getRange(2, urlCol, resultUrls.length).setValues(resultUrls);
}

FLASH PROFESSIONAL CS6 using ACTIONSCRIPT 3

var theXML:XML = new XML();
theXML.ignoreWhite = true;
theXML.onLoad = function()
{
var nodes = this.firstChild.childNodes;
for(i = 0; i < nodes.length; i++)
{
theList.addItem(nodes[i].firstChild.nodeValue, i);
}
};
theXML.load("http://localhost/xampp/phpflash/account.php");
These are the errors:
-1120: Access of undefined property i.
-1137: Incorrect number of arguments. Expected no more than 1.
I'm so newbie on this subject. Please help me with your kind and consideration.
1: you should declare i.
2: addItem function should only have one argument.
var theXML:XML = new XML();
theXML.ignoreWhite = true;
theXML.onLoad = function()
{
var nodes = this.firstChild.childNodes;
for(var i:int = 0; i < nodes.length; i++)
{
theList.addItem(nodes[i].firstChild.nodeValue);
}
};
theXML.load("http://localhost/xampp/phpflash/account.php");

How to do a very simple for loop

I want to accomplish this in coffeescript:
for (i = 0, i < count, i++) {
doSomething();
} // count is a number
But it is always handled as an array.
Is there way to prevent this? I have tried a while loop, but it didnĀ“t work out for me.
Thanks!
First of all, you want to implement this:
for(i = 0; i < count; i++) { ... }
in CoffeeScript, not for(i = 0, ...).
There's an example of this sort of thing right in the documentation:
countdown = (num for num in [10..1])
and the JavaScript version:
var countdown, num;
countdown = (function() {
var _i, _results;
_results = [];
for (num = _i = 10; _i >= 1; num = --_i) {
_results.push(num);
}
return _results;
})();
So you just want to use this:
for i in [1..count]
doSomething()
or
doSomething() for i in [1..count]
Note the the range starts at 1 so that [1..count] gives you count iterations.