⬆️ ⬇️

Cat purring in javascript

Recently, the idea to generate a cat's purr in javascript. Searching this topic on the Internet - nothing was found, it means you need to implement!

Investigating how this can be done at all - decided to use such a wonderful thing as the Web Audio API .

Modern browsers support this technology more or less normally.



The idea of ​​the Web Audi API is extremely simple - we create nodes and connect them to each other, with each node being a sound source, signal generator, delay, analyzer, filter, volume control, “output” point (from which the sound already falls into the sound card). ).



At first I took a purr mp3`shku, and decided to analyze the spectrum in order to further synthesize something similar. The connection scheme was as follows:

image

But the resulting beautiful signal directives did not help me at all. Therefore it was necessary to select parameters for generating experimentally.

')

To generate sound, the following scheme was implemented:

image



And so, my algorithm:

1. Create global volume control nodes, analyzer, output

2. Create blocks with nodes - generators, loudness, filters, delays:

image

3. We connect everything to the "main" volume

4. Sinusoidal “master” volume control



Thus, the following parameters were experimentally selected for purring:

1st block: rectangular, sinusoidal, sawtooth signal at 11 Hz with a low pass filter at 600 Hz and a delay of 0.001 s

2nd block: same signals with 0.0013 delay

2nd block: same signals with 0.0012 delay



It turned out continuous purring. But cats purr loudly on the exhale, and quietly on the inhale, so you need to periodically increase and decrease the volume. For this, I took a sine wave with a maximum volume limit:

image



The result was a purr of a “spherical cat in a vacuum” - very even, and as if the microphone was held right next to the cat's mouth, but very similar to the one I had in mp3 for example. Of course, all cats purr differently (and mine also purrs not so), but I am still pleased with the result.



Jsfiddle link



UPD. It doesn't work in firefox. The chrome works.

UPD 2. As it turned out, the sound is very different in different headphones. In some it sounds like a purr, in others it sounds like a helicopter!

UPD 3. Variant of purring from the comments: jsfiddle - in my opinion more like the present!

UPD 4. Corrected Fidl - set the settings selected by the user grinchy

Source: https://habr.com/ru/post/206734/



All Articles