js webaudio音量_WebAudio:JS中的振荡器

news/2024/7/24 12:35:42 标签: java, javascript, php, js, 区块链
<a class=js webaudio音量" width="403px" height="256px" style="outline: none;" />

js webaudio音量

How about generating some noise in JavaScript?

如何在JavaScript中产生一些噪音?

Demo is here: oscillator.

演示在这里:振荡器。

这是如何运作的? (How does this work?)

Using HTML Web Audio you can synthesize audio with a given frequency. E.g. 440Hz is A ("la" in solfège)

使用HTML Web Audio,您可以合成给定频率的音频。 例如440Hz为A(在solfège中为“ la”)

This means you don't need an <audio> element or any mp3, ogg, wav, etc, no external files.

这意味着您不需要<audio>元素或任何mp3,ogg,wav等文件,也不需要外部文件。

Let's see how.

让我们看看如何。

能够使用浏览器? (Capable browser?)

You need a browser that supports AudioContext. No such (major) browser at the time of writing, afaik. But there's webkitAudioContext supported in stable iOS Safari, Safari, Chrome. Also there could be browsers that support AudioContext but not the oscillator part. So starting off:

您需要支持AudioContext的浏览器。 在撰写本文时,afaik没有此类(主要)浏览器。 但是稳定的iOS Safari,Safari,Chrome中支持webkitAudioContext 。 也可能有一些浏览器支持AudioContext但不支持振荡器部分。 所以开始:

  // globals
  var audio_context, oscillator;
 
  // hello audio world
  (function init(g){
    try {
      audio_context = new (g.AudioContext || g.webkitAudioContext);
      oscillator = audio_context.createOscillator();
    } catch (e) {
      alert('No web audio oscillator support in this browser');
    }
  }(window));

开始/停止播放 (Start/stop playing)

Alright, next is a play(frequency /*number*/) function which makes noise with a given frequency.

好吧,接下来是一个play(frequency /*number*/)函数,它以给定的频率发出噪声。

  function play(freq) {
    oscillator = audio_context.createOscillator();
    oscillator.frequency.value = freq;
    oscillator.connect(audio_context.destination);
    oscillator.noteOn(0);
    fire('play', oscillator.frequency.value);
  }

(Don't mind fire(), it's just a poor man's event utility for logging what's going on)

(不要介意fire() ,它只是一个可怜的事件实用程序,用于记录正在发生的事情)

The audio context provides a createOscillator(). You assign the frequency you need and connect this oscillator node to the audio destination (speaker).

音频上下文提供了createOscillator() 。 您分配所需的频率,然后将此振荡器节点连接到音频目的地(扬声器)。

There is a nice analogy going on in the Web Audio: you start with some input noise, say coming from microphone or an audio file, or, in this case, you generate the noise yourself. Then you connect that initial input to the output (destination) which is the system speaker/phones. In between though you can pass the noise through a bunch of nodes that can modify the noise.

Web音频中有一个很好的类比:您从输入噪声开始,例如来自麦克风或音频文件,或者在这种情况下,您自己产生了噪声。 然后,将该初始输入连接到系统扬声器/电话的输出(目的地)。 在这两者之间,您可以通过一系列可以修改噪声的节点传递噪声。

In this simple example I only have an oscillator node which is connected directly to the audio destination.

在这个简单的示例中,我只有一个振荡器节点,该节点直接连接到音频目标。

noteOn(0) starts playing the noise we just generated.

noteOn(0)开始播放刚刚生成的噪音。

Implementing stop() to silence the noise is just a question of calling noteOff(0) on the same oscillator node.

实现stop()使噪声静音只是在同一振荡器节点上调用noteOff(0)的问题。

  function stop() {
    oscillator.noteOff(0);
    fire('stop');
  }

That's it, go play with the demo.

就这样,开始演示。

The demo plays 440Hz (A on 4th octave of the piano) and 880Hz (A on 5th octave) and also lets you punch in a number and see what happens. Probably nice to play with your dog and with sounds at frequencies you cannot hear.

该演示播放440Hz(钢琴第4个八度)和880Hz(第5个八度)。您还可以输入一个数字,看看会发生什么。 与您的狗一起玩,并以您听不到的频率听起来很不错。

和弦 (A chord)

Finally, an attempt to play a chord: three frequencies at the same time. C major is C, E and G tones. We have an array of the three frequencies, so loop over the array and create and noteOn three oscillator nodes.

最后,尝试弹奏一个和弦:同时三个频率。 C大调是C,E和G音调。 我们有三个频率的数组,因此在数组上循环并创建并noteOn三个振荡器节点。

  var cmajor = {};
  cmajor.yo = function () {
    var oscs = [], o, i, freqs = [261.63, 329.63, 392];
    freqs.forEach(function(freq) {
      o = audio_context.createOscillator();
      o.frequency.value = freq;
      o.connect(audio_context.destination);
      o.noteOn(0);
      oscs.push(o);
    });
    this.oscs = oscs;
    fire('play', '\n - ' + freqs.join('Hz\n - '));
  };
  
  cmajor.no = function () {
    this.oscs.forEach(function(o) {
      o.noteOff(0);
    });
    fire('stop');
  };

谢谢 (Thanks)

Some links for learning more

一些链接以了解更多

Once again the demo is here: oscillator.

演示再次在这里:振荡器。

Intro: html5rocks.com

简介: html5rocks.com

Educational demos: webaudiodemos.appspot.com/

教育演示: webaudiodemos.appspot.com/

Tell your friends about this post on Facebook and Twitter

在Facebook和Twitter上告诉您的朋友有关此帖子的信息

翻译自: https://www.phpied.com/webaudio-oscillator-in-js/

js webaudio音量


http://www.niftyadmin.cn/n/1305996.html

相关文章

yslow_YSlow开发:设置

yslowAs promised, lets setup for YSlow development using the easiest option - the bookmarklet version. The journey of conquering the world with your rules and extensions... starts with the first step. 如所承诺的&#xff0c;让我们使用最简单的选项-小书签版本…

文化甲板_甲板大厅2012

文化甲板Traditions are traditions. Marry Christmas with a new cover, this time its Deck the Halls: 传统是传统。 与圣诞节一起换上新的封面&#xff0c;这次是Deck the Halls &#xff1a; Deck the halls (part one) 甲板大厅(第一部分) (Its only part one because I …

实时 webaudio_WebAudio:实时输入

实时 webaudioLive input, aka getUserMedia: it exists in Chrome Canary for audio too. Great times to be a web developer, right? 实时输入&#xff0c;也称为getUserMedia &#xff1a;Chrome Canary中也存在音频输入。 成为网络开发人员的美好时光&#xff0c;对吗&am…

getusermedia_Opera 12中的getUserMedia

getusermediaOpera 12 wins - the first stable desktop browser to ship getUserMedia(). I believe they had shipped it already in a mobile version of the browser. Opera 12获胜-第一个稳定的桌面浏览器发布getUserMedia() 。 我相信他们已经在浏览器的移动版本中发布了…

css变量_CSS变量

css变量Weeee, CSS variables just landed in WebKit, this is pretty exciting! Weeee&#xff0c;CSS变量仅位于WebKit中&#xff0c;这非常令人兴奋&#xff01; Unfortunately I couldnt see them in action in WebKit nightly (must be something Im missing), but theyre…

俄窃贼当众盗走名画_公开的;当众

俄窃贼当众盗走名画Update: WebPerfSummit 更新&#xff1a; WebPerfSummit Update 2: SydJS, Anaconda LimoMolly Malones, qCon confirmed, fix *some* Portuguese spelling 更新2&#xff1a; SydJS&#xff0c; Anaconda Limo Molly Malones&#xff0c;已确认qCon&a…

Several ports (8005, 8080, 8009) required by Tomcat v7.0 Server at localhost are already in use

1 错误描述 Several ports (8005, 8080, 8009) required by Tomcat v7.0 Server at localhost are already in use. The server may already be running in another process, or a system process may be using the port. To start this server you will need to stop the ot…

java.lang.ClassNotFoundException: org.apache.catalina.startup.VersionLoggerListener

1 错误描述 一月 02, 2015 3:48:30 下午 org.apache.tomcat.util.digester.Digester startElement 严重: Begin event threw exception java.lang.ClassNotFoundException: org.apache.catalina.startup.VersionLoggerListenerat java.net.URLClassLoader$1.run(URLClassLoader…