Home > Web Front-end > H5 Tutorial > Resource recommendations for HTML5 music visualization video tutorials

Resource recommendations for HTML5 music visualization video tutorials

黄舟
Release: 2017-09-01 14:37:39
Original
2498 people have browsed it

Is the simple music playback too monotonous? If you can also see the music while listening to it, it will be more interesting. This course will lead you to use webAudio and canvas to visualize your music in the form you like and make your music move.

Resource recommendations for HTML5 music visualization video tutorials

Course playback address: //m.sbmmt.com/course/327.html

The Teacher's teaching style:

The teacher's lectures are simple and in-depth, clear in structure, analyzed layer by layer, interlocking, rigorous in argumentation, and rigorous in structure. He uses the logical power of thinking to attract students' attention and controls the classroom with reason. teaching process. By listening to the teacher's lectures, students not only learn knowledge, but also receive thinking training, and are also influenced and influenced by the teacher's rigorous academic attitude

The more difficult point in this video is HTML5 music visualization:

Music acquisition and playback

Building the front-end and back-end of the application

1, create a new media data folder, public/media, put the audio data into it
2, build the page CSS framework, /public/stylesheets/index.css
3, read the page content, views/index.ejs
4, background routing Control, routes/index.js, get the music list and return it to the previous section

ajax request server-side audio data

Create a new file index.js under javascripts, reference and create it in views/index.ejs File

<script type="text/javascript" src="/javascripts/index.js"></script>
Copy after login

Edit and create files to achieve click effects

<ul id="list">
        <% music.forEach(function(name){ %>
              <li title="<%= name %>"><%= name %></li>    #设置title属性
        <% }) %>
</ul>
Copy after login


Decode and play audio

AudioContext

An object containing each AudioNode object and their connections, that is, the audio context object. There is only one AudioContext created in a document: var ac = new window.AudioContext();

Attributes:

destination, AudioDestinationNode object, where all audio outputs gather, equivalent to audio hardware, All AudioNodes are directly or indirectly connected to here.

currentTime, the time (seconds) from the creation of AudioContext to the current time.

Method:

decodeAudioData(arrayBuffer,succ(buffer),err), asynchronously decode audio data contained in arrayBuffer

createBufferSource(), create autoBufferSourceNode object

createAnalyser(), create AnalyserNode object

createGain()/createGainNode(), create GainNode object

AudioBufferSourceNode

represents an audio resource in memory, its audio Data is stored in AudioBuffer (its buffer attribute)
Creation: var buffersource = ac.createBufferSource();

Properties:

buffer, AudioBuffer object, represents the audio resource data to be played
——Sub-attribute: duration, the duration of the audio resource (seconds)

loop, whether to loop playback, default false

onended, can be bound to the time called when the audio playback is completed Handler

Method:

start/noteOn(when=ac.currentTime,offset=0,buration=buffer.duration-offset), start playing audio.
when: when to start playing;
offset: how many seconds to start playing the audio;
duration: how many seconds to play

stop/noteOff(when=ac.currentTime), end Play audio

Add volume control

GainNode

An object that changes the audio volume and changes the signal strength of all sampleframes passing its audio data
Create: var gainNode = ac.createGain()/ac.createGainNode();

gain, AudioParam object, the intensity of the audio signal can be changed by changing its value. The default value attribute value is 1, and the minimum value is 0. The maximum value is 1, and its value can also be greater than 1 and less than 0

Playback bug fix

Problem: When playing the second song, the first song is still playing. The main reason is Each time you click on the music list, load("/media/"+this.title) is called, the data is decoded and played:

xhr.onload = function(){
    ac.decodeAudioData(xhr.response, function(buffer){
        var bufferSource = ac.createBufferSource();
        bufferSource.buffer = buffer;
        bufferSource.connect(gainNode);
        bufferSource[bufferSource.start?"start":"noteOn"](0);
    }, function(err){
                console.log(err);
    });
}
Copy after login

Solution:
Assign a null value to the audio data var source = null;, Save the decoded data of the previous song source = bufferSource;, and judge the execution to stop playing source && source[source.stop? "stop" : "noteoff"](0);

Music data visualization

AnalyserNode

音频分析对象,它能实时的分析音频资源的频域和时域信息,但不会对音频流做任何处理
创建:var analyser = ac.createAnalyser();

fftSize,设置FFT(FFT是离散傅里叶变换的快速算法,用于将一个信号变换到频域)值得大小,用于分析得到频域,为32 - 2048之间2的整数倍,默认为2048。实时得到的音频频域的数据个数为FFTSize的一半

frequencyBinCount,FFT值得一半,即实时得到的音频频域的数据个数

getByteFrequencyData(Uint8Array),复制音频当前的频域数据(数量是FrequencyBinCount)到Uint8Array(8位无符号整型类型化数组)中

创建Analyser对象:

var analyser = ac.createAnalyser();
analyser.fftSize = 512;
analyser.connect(gainNode);
Copy after login

连接到分

析对象获取数据:bufferSource.connect(analyser);

实现可视化功能函数:

function visualizer(){
    var arr = new Uint8Array(analyser.frequencyBinCount);
    analyser.getByteFrequencyData(arr);
    console.log(arr);
}
Copy after login

调用visualizer函数:

利用canvas将音乐数据可视化(柱状图)

在views下加入id

控制高度变化:

var box = $("#box")[0];
var height, width;
var canvas = document.createElement("canvas");
box.appendChild(canvas);
 
function resize(){
    height = box.clientHeight;
    width = box.clientWidth;
    canvas.height = height;
    canvas.width = width;
}
resize();    #调用触发函数
 
window.onresize = resize;
Copy after login

利用canvas将音乐数据可视化(圆点图)

应用优化

webAudio API

webAudio核心功能封装为对象

The above is the detailed content of Resource recommendations for HTML5 music visualization video tutorials. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template