Baseline Widely available *
Web audio API - моÑнÑй и многогÑаннÑй инÑÑÑÑÐ¼ÐµÐ½Ñ Ð´Ð»Ñ Ð¼Ð°Ð½Ð¸Ð¿ÑлÑÑии звÑковой ÑоÑÑавлÑÑÑей на веб-ÑÑÑаниÑе, ÑÑо даÑÑ Ð²Ð¾Ð·Ð¼Ð¾Ð¶Ð½Ð¾ÑÑÑ ÑазÑабоÑÑикам вÑбÑаÑÑ Ð¸ÑÑоÑники, добавиÑÑ Ðº ним ÑпеÑиалÑнÑе звÑковÑе ÑÑÑекÑÑ (Ñакие как panning), визÑализиÑоваÑÑ Ð¸Ñ Ð¸ многое дÑÑгое.
ÐбÑие конÑепÑии и иÑполÑзование Web AudioWeb audio API позволÑÐµÑ Ð¾Ð±ÑабаÑÑваÑÑ Ð¾Ð¿ÐµÑаÑии над аÑдио Ñ Ð¿Ð¾Ð¼Ð¾ÑÑÑ ÑпеÑиалÑного аÑдио конÑекÑÑа (audio context), и бÑл ÑпÑоекÑиÑован на иÑполÑзование модÑлÑной маÑÑÑÑÑизаÑии (modular routing). ÐазовÑе опеÑаÑии вÑполнÑÑÑÑÑ Ñ Ð¿Ð¾Ð¼Ð¾ÑÑÑ Ð°Ñдио Ñзлов (audio nodes), коÑоÑÑе обÑединÑÑÑÑÑ Ð²Ð¼ÐµÑÑе, ÑоÑмиÑÑÑ Ð°Ñдио-маÑÑÑÑÑизаÑоÑÑ *ÑаблиÑÑ (audio routing graph). ÐеÑколÑко иÑÑоÑников - Ñ ÑазнÑми видами поÑоÑнÑÑ ÑÑ ÐµÐ¼ - поддеÑживаÑÑÑÑ Ð´Ð°Ð¶Ðµ изнÑÑÑи пÑоÑÑого конÑекÑÑа. ÐÑа модÑлÑÐ½Ð°Ñ ÐºÐ¾Ð½ÑепÑÐ¸Ñ Ð¾Ð±ÐµÑпеÑÐ¸Ð²Ð°ÐµÑ Ð³Ð¸Ð±ÐºÐ¾ÑÑÑ Ð² Ñоздании ÑложнÑÑ ÑÑнкÑий Ð´Ð»Ñ Ð´Ð¸Ð½Ð°Ð¼Ð¸ÑеÑÐºÐ¸Ñ ÑÑÑекÑов.
ÐÑдио ÑÐ·Ð»Ñ Ð¾Ð±ÑединÑÑÑÑÑ Ð² Ñепи и пÑоÑÑÑе ÑеÑи Ð¸Ñ Ð²Ð²Ð¾Ð´Ð°Ð¼Ð¸ и вÑводами. Ðни, как пÑавило, запÑÑкаÑÑÑÑ Ñ Ð¾Ð´Ð½Ð¸Ð¼ или более иÑÑоÑниками. ÐÑÑоÑники пÑедÑÑавлÑÑÑ Ñобой маÑÑÐ¸Ð²Ñ ÑÑмплов на единиÑÑ Ð²Ñемени. ÐапÑимеÑ, пÑи ÑаÑÑоÑе диÑкÑеÑизаÑии 44100 ÐÑ, в каждой ÑекÑнде каждого канала ÑаÑположено 22050 ÑÑмплов. Ðни могÑÑ Ð±ÑÑÑ Ð»Ð¸Ð±Ð¾ обÑабоÑÐ°Ð½Ñ Ð¼Ð°ÑемаÑиÑеÑки (ÑмоÑÑиÑе : OscillatorNode), либо ÑÑиÑÐ°Ð½Ñ Ñ Ð·Ð²Ñко/видео Ñайлов (ÑмоÑÑиÑе : AudioBufferSourceNode и MediaElementAudioSourceNode) или Ñ Ð°Ñдио поÑоков (ÑмоÑÑиÑе : MediaStreamAudioSourceNode). Ðо ÑакÑÑ, звÑковÑе ÑÐ°Ð¹Ð»Ñ - пÑоÑÑо запиÑÑ Ð·Ð²ÑковÑÑ ÐºÐ¾Ð»ÐµÐ±Ð°Ð½Ð¸Ð¹, коÑоÑÑе поÑÑÑпаÑÑ Ð¾Ñ Ð¼Ð¸ÐºÑоÑона и мÑзÑкалÑнÑÑ Ð¸Ð½ÑÑÑÑменÑов, ÑмеÑиваÑÑÑ Ð² Ð¾Ð´Ð½Ñ ÑложнÑÑ Ð²Ð¾Ð»Ð½Ñ. ÐÑводнÑе даннÑе ÑÑÐ¸Ñ Ñзлов могÑÑ Ð±ÑÑÑ ÑвÑÐ·Ð°Ð½Ñ Ñ Ð²Ð²Ð¾Ð´Ð½Ñми дÑÑÐ³Ð¸Ñ , ÑÑо ÑмеÑиваÑÑ Ð¸Ð»Ð¸ модиÑиÑиÑÑÑÑ Ð¿Ð¾Ñоки звÑковÑÑ Ð¾Ð±ÑазÑов в инÑе поÑоки. ÐопÑлÑÑÐ½Ð°Ñ Ð¼Ð¾Ð´Ð¸ÑикаÑÐ¸Ñ - Ñмножение обÑазÑа на знаÑение, ÑÑÐ¾Ð±Ñ ÑделаÑÑ Ð²ÑÑ Ð¾Ð´Ð½Ð¾Ð¹ звÑк менее или более гÑомким (ÑмоÑÑиÑе : GainMode). Ðогда звÑк бÑл ÑÑпеÑно обÑабоÑан пÑедназнаÑеннÑм ÐµÐ¼Ñ ÑÑÑекÑом, он Ð¼Ð¾Ð¶ÐµÑ Ð±ÑÑÑ Ð¿ÑивÑзан к вÑÑ Ð¾Ð´Ð½Ð¾Ð¼Ñ Ð¿Ð¾ÑÐ¾ÐºÑ (ÑмоÑÑиÑе : AudioContext.destination), ÑÑо напÑавлÑÐµÑ Ð·Ð²Ñк в динамики или микÑоÑон. ÐаннÑй Ñаг нÑжен лиÑÑ ÐµÑли Ð²Ñ Ð¿ÑедпоÑÑÑÑе даÑÑ Ð²Ð¾Ð·Ð¼Ð¾Ð¶Ð½Ð¾ÑÑÑ Ð¿Ð¾Ð»ÑзоваÑÐµÐ»Ñ ÑÑлÑÑаÑÑ Ð²Ð°Ñи ÑедевÑÑ.
ÐÑоÑÑой, ÑипиÑнÑй поÑÑдок дейÑÑвий вÑÐ¿Ð¾Ð»Ð½ÐµÐ½Ð¸Ñ Ð¼Ð°Ð½Ð¸Ð¿ÑлÑÑий над аÑдио вÑглÑÐ´Ð¸Ñ Ñак :
РаÑпÑеделение вÑемени конÑÑолиÑÑеÑÑÑ Ñ Ð²ÑÑокой ÑоÑноÑÑÑÑ Ð¸ низкими задеÑжками, позволÑÑ ÑазÑабоÑÑикам пиÑаÑÑ ÐºÐ¾Ð´, ÑÑо ÑоÑно ÑеагиÑÑÐµÑ Ð½Ð° ÑобÑÑÐ¸Ñ Ð¸ в ÑоÑÑоÑнии обÑабоÑаÑÑ Ð¾Ð±ÑÐ°Ð·ÐµÑ Ð´Ð°Ð¶Ðµ на вÑÑокой оÑенке обÑазÑов (sample rate). Так ÑÑо Ñакие пÑÐ¸Ð»Ð¾Ð¶ÐµÐ½Ð¸Ñ ÐºÐ°Ðº ÑиÑм-компÑÑÑÐµÑ Ð¸ пÑогÑаммнÑй авÑÐ¾Ð¼Ð°Ñ Ð²Ñегда под ÑÑкой.
Web audio API Ñакже даÑÑ Ð½Ð°Ð¼ возможноÑÑÑ ÐºÐ¾Ð½ÑÑолиÑоваÑÑ Ñо, каким аÑдио ÑвлÑеÑÑÑ Ð² пÑоÑÑÑанÑÑве. ÐÑполÑзÑÑ Ð¾ÑобÑÑ ÑиÑÑемÑ, ÑÑо базиÑÑеÑÑÑ Ð½Ð° модели source-listener, он позволÑÐµÑ ÐºÐ¾Ð½ÑÑолиÑоваÑÑ Ð¼Ð¾Ð´ÐµÐ»Ñ Ð¿Ð°Ð½Ð¾ÑамиÑÐ¾Ð²Ð°Ð½Ð¸Ñ Ð¸ Ð¾Ð±Ñ Ð¾Ð´Ð¸ÑÑÑÑ Ð±ÐµÐ· диÑÑанÑионно-вÑзванного оÑÐ»Ð°Ð±Ð»ÐµÐ½Ð¸Ñ (distance-induced attenuation) или duppler shift, вÑзванного Ñдвигом иÑÑоÑника (или Ñдвигом ÑлÑÑаÑелÑ).
ÐÑимеÑание: ÐÑ Ð¼Ð¾Ð¶ÐµÑе пÑоÑиÑаÑÑ Ð±Ð¾Ð»ÐµÐµ деÑалÑнÑй ÑеоÑеÑиÑеÑкий маÑеÑиал о Web audio API в наÑей ÑÑаÑÑе Basic concepts behind Web Audio API.
Web Audio API инÑеÑÑейÑÑÐ Web audio API еÑÑÑ Ð²Ñего 28 инÑеÑÑейÑов и ÑооÑвеÑÑÑвÑÑÑÐ¸Ñ ÑобÑÑий, коÑоÑÑе Ð¼Ñ ÑгÑÑппиÑовали в 9 каÑегоÑий по ÑÑнкÑионалÑноÑÑи.
ÐлавнÑе обÑекÑÑ Ð¾Ð¿ÑÐµÐ´ÐµÐ»ÐµÐ½Ð¸Ñ Ð°ÑдиоÐлавнÑе конÑейнеÑÑ Ð¸ опÑеделениÑ, ÑÑо ÑоÑмиÑÑÑÑ Ð°Ñдио обÑÐµÐºÑ Ð² Web Audio API.
AudioContext
AudioContext пÑедÑÑавлÑÐµÑ Ñобой некий аÑдио обÑабаÑÑваÑÑий обÑекÑ, ÑооÑÑжÑннÑй из аÑдио модÑлей ÑвÑзаннÑÑ
вмеÑÑе, где каждÑй ÑвлÑеÑÑÑ ÑкземплÑÑом клаÑÑа AudioNode
. AudioContext ÑпÑавлÑÐµÑ Ñозданием Ñзлов, ÑодеÑжаÑиÑ
ÑÑ Ð² нÑм и вÑполнÑÐµÑ Ð¾Ð±ÑабоÑÐºÑ Ð¸Ð»Ð¸ декодиÑование аÑдио даннÑÑ
.
AudioNode
ÐнÑеÑÑÐµÐ¹Ñ AudioNode пÑедÑÑавлÑÐµÑ Ñобой некий обÑабаÑÑваÑÑий модÑÐ»Ñ Ñакой как иÑÑоÑник аÑдио (Ñо еÑÑÑ HTML <audio> или <video> ÑлеменÑ), конеÑнÑй аÑдио обÑекÑ, модÑÐ»Ñ Ð½ÐµÐ¿Ð¾ÑÑедÑÑвенной обÑабоÑки (напÑÐ¸Ð¼ÐµÑ ÑилÑÑÑ BiquadFilterNode или звÑковой конÑÑÐ¾Ð»Ð»ÐµÑ Ñакой как GainNode).
AudioParam
ÐнÑеÑÑÐµÐ¹Ñ AudioParam
пÑедÑÑавлÑÐµÑ Ð°Ñдио-паÑамеÑÑÑ ÑвÑзаннÑе Ñ AudioNode
. Ðн Ð¼Ð¾Ð¶ÐµÑ ÑодеÑжаÑÑ ÐºÐ°Ðº ÑоÑное знаÑение паÑамеÑÑа, Ñак и паÑамеÑÑÑ Ð¸Ð·Ð¼ÐµÐ½ÑÑÑиеÑÑ Ð²Ð¾ вÑемени.
ended
(event)
СобÑÑие ended генеÑиÑÑеÑÑÑ Ñогда, когда воÑпÑоизведение оÑÑановлено по пÑиÑине оконÑÐ°Ð½Ð¸Ñ Ð½Ð¾ÑиÑелÑ.
ÐнÑеÑÑейÑÑ, коÑоÑÑе опÑеделÑÑÑ Ð¸ÑÑоÑники звÑка Ð´Ð»Ñ Ð¸ÑполÑÐ·Ð¾Ð²Ð°Ð½Ð¸Ñ Ð² Web Audio API.
OscillatorNode
OscillatorNode
пÑедÑÑавлÑÐµÑ Ñобой иÑÑоÑник ÑинÑÑоидалÑной волнÑ. ÐÑо модÑÐ»Ñ AudioNode
коÑоÑÑй пÑÐ¸Ð½Ð¸Ð¼Ð°ÐµÑ Ð½Ð° вÑ
од ÑаÑÑоÑÑ Ð¸ генеÑиÑÑÐµÑ ÑинÑÑоидалÑнÑÑ Ð²Ð¾Ð»Ð½Ñ Ñ ÑÑой ÑаÑÑоÑой.
AudioBuffer
ÐнÑеÑÑÐµÐ¹Ñ AudioBuffer
пÑедÑÑавлÑÐµÑ Ñобой коÑоÑкий аÑдио ÑÑмпл наÑ
одÑÑийÑÑ Ð² памÑÑи, ÑозданнÑй из аÑдио Ñайла Ñ Ð¸ÑполÑзованием меÑода AudioContext.decodeAudioData()
, или ÑозданнÑй из необÑабоÑаннÑÑ
(ÑÑÑÑÑ
) даннÑÑ
Ñ Ð¸ÑполÑзованием меÑода AudioContext.createBuffer()
. ÐоÑле декодиÑÐ¾Ð²Ð°Ð½Ð¸Ñ Ð°Ñдио даннÑе могÑÑ Ð±ÑÑÑ Ð¿Ð¾Ð¼ÐµÑÐµÐ½Ñ Ð² AudioBufferSourceNode
.
AudioBufferSourceNode
ÐнÑеÑÑÐµÐ¹Ñ AudioBufferSourceNode
пÑедÑÑавлÑÐµÑ Ñобой иÑÑоÑник звÑка, ÑоÑÑоÑÑий из аÑдио даннÑÑ
, Ñ
ÑанÑÑиÑ
ÑÑ Ð² AudioBuffer
. ÐÑо AudioNode
, коÑоÑÑй дейÑÑвÑÐµÑ Ð² каÑеÑÑве иÑÑоÑника звÑка.
MediaElementAudioSourceNode
ÐнÑеÑÑÐµÐ¹Ñ MediaElementAudioSourceNode
пÑедÑÑавлÑÐµÑ Ð¸ÑÑоÑник звÑка, ÑодеÑжаÑегоÑÑ Ð² ÑлеменÑе HTML5 <audio>
или <video>
. ÐÑо AudioNode
, коÑоÑÑй дейÑÑвÑÐµÑ Ð² каÑеÑÑве иÑÑоÑника звÑка.
MediaStreamAudioSourceNode
ÐнÑеÑÑÐµÐ¹Ñ MediaStreamAudioSourceNode
пÑедÑÑавлÑÐµÑ Ð¸ÑÑоÑник звÑка, ÑодеÑжаÑегоÑÑ Ð² поÑоке WebRTC MediaStream
(напÑÐ¸Ð¼ÐµÑ Ð²ÐµÐ±-камеÑÑ Ð¸Ð»Ð¸ микÑоÑона). ÐÑо AudioNode
, коÑоÑÑй дейÑÑвÑÐµÑ Ð² каÑеÑÑве иÑÑоÑника звÑка.
ÐнÑеÑÑейÑÑ Ð´Ð»Ñ Ð¾Ð¿ÑÐµÐ´ÐµÐ»ÐµÐ½Ð¸Ñ ÑÑÑекÑов коÑоÑÑе можно пÑимениÑÑ Ðº иÑÑоÑникам звÑка.
BiquadFilterNode
BiquadFilterNode
пÑедÑÑавлÑÐµÑ Ð¿ÑоÑÑой ÑилÑÑÑ Ð½Ð¸Ð·ÐºÐ¾Ð³Ð¾ поÑÑдка. ÐÑо AudioNode
пÑедÑÑавлÑÑÑий ÑазлиÑнÑе Ð²Ð¸Ð´Ñ ÑилÑÑÑов Ñакие как ÑÐºÐ²Ð°Ð»Ð°Ð¹Ð·ÐµÑ Ð¸Ð»Ð¸ ÑпÑавление ÑоналÑноÑÑÑÑ. BiquadFilterNode
Ð¸Ð¼ÐµÐµÑ Ñовно один вÑ
од и один вÑÑ
од.
ConvolverNode
ConvolverNode
ÑÑо AudioNode
вÑполнÑÑÑий линейнÑÑ ÑвÑÑÑÐºÑ AudioBuffer, ÑаÑÑо пÑименÑеÑÑÑ Ð´Ð»Ñ Ð´Ð¾ÑÑÐ¸Ð¶ÐµÐ½Ð¸Ñ ÑÑÑекÑа ÑевеÑбеÑаÑии по заданной импÑлÑÑной Ñ
аÑакÑеÑиÑÑике.
DelayNode
DelayNode
пÑедÑÑавлÑÐµÑ Ñобой Ð»Ð¸Ð½Ð¸Ñ Ð·Ð°Ð´ÐµÑжки (delay-line); ÑодеÑÐ¶Ð¸Ñ AudioNode
вÑзÑваÑÑий задеÑÐ¶ÐºÑ Ð¼ÐµÐ¶Ð´Ñ Ð²ÑÑ
однÑм и вÑ
однÑм Ñигналом.
DynamicsCompressorNode
The DynamicsCompressorNode
interface provides a compression effect, which lowers the volume of the loudest parts of the signal in order to help prevent clipping and distortion that can occur when multiple sounds are played and multiplexed together at once.
GainNode
The GainNode
interface represents a change in volume. It is an AudioNode
audio-processing module that causes a given gain to be applied to the input data before its propagation to the output.
StereoPannerNode
The StereoPannerNode
interface represents a simple stereo panner node that can be used to pan an audio stream left or right.
WaveShaperNode
The WaveShaperNode
interface represents a non-linear distorter. It is an AudioNode
that use a curve to apply a waveshaping distortion to the signal. Beside obvious distortion effects, it is often used to add a warm feeling to the signal.
PeriodicWave
Used to define a periodic waveform that can be used to shape the output of an OscillatorNode
.
Once you are done processing your audio, these interfaces define where to output it.
AudioDestinationNode
The AudioDestinationNode
interface represents the end destination of an audio source in a given context â usually the speakers of your device.
MediaStreamAudioDestinationNode
The MediaStreamAudioDestinationNode
interface represents an audio destination consisting of a WebRTC MediaStream
with a single AudioMediaStreamTrack
, which can be used in a similar way to a MediaStream obtained from Navigator.getUserMedia
. It is an AudioNode
that acts as an audio destination.
If you want to extract time, frequency and other data from your audio, the AnalyserNode
is what you need.
AnalyserNode
The AnalyserNode
interface represents a node able to provide real-time frequency and time-domain analysis information, for the purposes of data analysis and visualization.
To split and merge audio channels, you'll use these interfaces.
ChannelSplitterNode
The ChannelSplitterNode
interface separates the different channels of an audio source out into a set of mono outputs.
ChannelMergerNode
The ChannelMergerNode
interface reunites different mono inputs into a single output. Each input will be used to fill a channel of the output.
These interfaces allow you to add audio spatialization panning effects to your audio sources.
AudioListener
The AudioListener
interface represents the position and orientation of the unique person listening to the audio scene used in audio spatialization.
PannerNode
The PannerNode
interface represents the behavior of a signal in space. It is an AudioNode
audio-processing module describing its position with right-hand Cartesian coordinates, its movement using a velocity vector and its directionality using a directionality cone.
If you want to use an external script to process your audio source, the below Node and events make it possible.
ÐÑимеÑание: As of the August 29 2014 Web Audio API spec publication, these features have been marked as deprecated, and are soon to be replaced by Audio_Workers.
ScriptProcessorNode
The ScriptProcessorNode
interface allows the generation, processing, or analyzing of audio using JavaScript. It is an AudioNode
audio-processing module that is linked to two buffers, one containing the current input, one containing the output. An event, implementing the AudioProcessingEvent
interface, is sent to the object each time the input buffer contains new data, and the event handler terminates when it has filled the output buffer with data.
audioprocess
(event)
The audioprocess
event is fired when an input buffer of a Web Audio API ScriptProcessorNode
is ready to be processed.
AudioProcessingEvent
The Web Audio API AudioProcessingEvent
represents events that occur when a ScriptProcessorNode
input buffer is ready to be processed.
It is possible to process/render an audio graph very quickly in the background â rendering it to an AudioBuffer
rather than to the device's speakers â with the following.
OfflineAudioContext
The OfflineAudioContext
interface is an AudioContext
interface representing an audio-processing graph built from linked together AudioNode
s. In contrast with a standard AudioContext
, an OfflineAudioContext
doesn't really render the audio but rather generates it, as fast as it can, in a buffer.
complete
(event)
The complete
event is fired when the rendering of an OfflineAudioContext
is terminated.
OfflineAudioCompletionEvent
The OfflineAudioCompletionEvent
represents events that occur when the processing of an OfflineAudioContext
is terminated. The complete
event implements this interface.
Audio workers provide the ability for direct scripted audio processing to be done inside a web worker context, and are defined by a couple of interfaces (new as of 29th August 2014.) These are not implemented in any browsers yet. When implemented, they will replace ScriptProcessorNode
, and the other features discussed in the Audio processing via JavaScript section above.
AudioWorkerNode
The AudioWorkerNode interface represents an AudioNode
that interacts with a worker thread to generate, process, or analyse audio directly.
AudioWorkerGlobalScope
The AudioWorkerGlobalScope
interface is a DedicatedWorkerGlobalScope
-derived object representing a worker context in which an audio processing script is run; it is designed to enable the generation, processing, and analysis of audio data directly using JavaScript in a worker thread.
AudioProcessEvent
This is an Event
object that is dispatched to AudioWorkerGlobalScope
objects to perform processing.
The following interfaces were defined in old versions of the Web Audio API spec, but are now obsolete and have been replaced by other interfaces.
JavaScriptNode
Used for direct audio processing via JavaScript. This interface is obsolete, and has been replaced by ScriptProcessorNode
.
WaveTableNode
Used to define a periodic waveform. This interface is obsolete, and has been replaced by PeriodicWave
.
This example shows a wide variety of Web Audio API functions being used. You can see this code in action on the Voice-change-o-matic demo (also check out the full source code at Github) â this is an experimental voice changer toy demo; keep your speakers turned down low when you use it, at least to start!
The Web Audio API lines are highlighted; if you want to find more out about what the different methods, etc. do, have a search around the reference pages.
var audioCtx = new (window.AudioContext || window.webkitAudioContext)(); // define audio context
// Webkit/blink browsers need prefix, Safari won't work without window.
var voiceSelect = document.getElementById("voice"); // select box for selecting voice effect options
var visualSelect = document.getElementById("visual"); // select box for selecting audio visualization options
var mute = document.querySelector(".mute"); // mute button
var drawVisual; // requestAnimationFrame
var analyser = audioCtx.createAnalyser();
var distortion = audioCtx.createWaveShaper();
var gainNode = audioCtx.createGain();
var biquadFilter = audioCtx.createBiquadFilter();
function makeDistortionCurve(amount) {
// function to make curve shape for distortion/wave shaper node to use
var k = typeof amount === "number" ? amount : 50,
n_samples = 44100,
curve = new Float32Array(n_samples),
deg = Math.PI / 180,
i = 0,
x;
for (; i < n_samples; ++i) {
x = (i * 2) / n_samples - 1;
curve[i] = ((3 + k) * x * 20 * deg) / (Math.PI + k * Math.abs(x));
}
return curve;
}
navigator.getUserMedia(
// constraints - only audio needed for this app
{
audio: true,
},
// Success callback
function (stream) {
source = audioCtx.createMediaStreamSource(stream);
source.connect(analyser);
analyser.connect(distortion);
distortion.connect(biquadFilter);
biquadFilter.connect(gainNode);
gainNode.connect(audioCtx.destination); // connecting the different audio graph nodes together
visualize(stream);
voiceChange();
},
// Error callback
function (err) {
console.log("The following gUM error occured: " + err);
},
);
function visualize(stream) {
WIDTH = canvas.width;
HEIGHT = canvas.height;
var visualSetting = visualSelect.value;
console.log(visualSetting);
if (visualSetting == "sinewave") {
analyser.fftSize = 2048;
var bufferLength = analyser.frequencyBinCount; // half the FFT value
var dataArray = new Uint8Array(bufferLength); // create an array to store the data
canvasCtx.clearRect(0, 0, WIDTH, HEIGHT);
function draw() {
drawVisual = requestAnimationFrame(draw);
analyser.getByteTimeDomainData(dataArray); // get waveform data and put it into the array created above
canvasCtx.fillStyle = "rgb(200, 200, 200)"; // draw wave with canvas
canvasCtx.fillRect(0, 0, WIDTH, HEIGHT);
canvasCtx.lineWidth = 2;
canvasCtx.strokeStyle = "rgb(0, 0, 0)";
canvasCtx.beginPath();
var sliceWidth = (WIDTH * 1.0) / bufferLength;
var x = 0;
for (var i = 0; i < bufferLength; i++) {
var v = dataArray[i] / 128.0;
var y = (v * HEIGHT) / 2;
if (i === 0) {
canvasCtx.moveTo(x, y);
} else {
canvasCtx.lineTo(x, y);
}
x += sliceWidth;
}
canvasCtx.lineTo(canvas.width, canvas.height / 2);
canvasCtx.stroke();
}
draw();
} else if (visualSetting == "off") {
canvasCtx.clearRect(0, 0, WIDTH, HEIGHT);
canvasCtx.fillStyle = "red";
canvasCtx.fillRect(0, 0, WIDTH, HEIGHT);
}
}
function voiceChange() {
distortion.curve = new Float32Array();
biquadFilter.gain.value = 0; // reset the effects each time the voiceChange function is run
var voiceSetting = voiceSelect.value;
console.log(voiceSetting);
if (voiceSetting == "distortion") {
distortion.curve = makeDistortionCurve(400); // apply distortion to sound using waveshaper node
} else if (voiceSetting == "biquad") {
biquadFilter.type = "lowshelf";
biquadFilter.frequency.value = 1000;
biquadFilter.gain.value = 25; // apply lowshelf filter to sounds using biquad
} else if (voiceSetting == "off") {
console.log("Voice settings turned off"); // do nothing, as off option was chosen
}
}
// event listeners to change visualize and voice settings
visualSelect.onchange = function () {
window.cancelAnimationFrame(drawVisual);
visualize(stream);
};
voiceSelect.onchange = function () {
voiceChange();
};
mute.onclick = voiceMute;
function voiceMute() {
// toggle to mute and unmute sound
if (mute.id == "") {
gainNode.gain.value = 0; // gain set to 0 to mute sound
mute.id = "activated";
mute.innerHTML = "Unmute";
} else {
gainNode.gain.value = 1; // gain set to 1 to unmute sound
mute.id = "";
mute.innerHTML = "Mute";
}
}
СпеÑиÑикаÑии СовмеÑÑимоÑÑÑ Ñ Ð±ÑаÑзеÑами СмоÑÑиÑе Ñакже Quicklinks
RetroSearch is an open source project built by @garambo | Open a GitHub Issue
Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo
HTML:
3.2
| Encoding:
UTF-8
| Version:
0.7.4