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.
Course playback address: http://www.php.cn/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>
Edit and create files to achieve click effects
<ul id="list"> <% music.forEach(function(name){ %> <li title="<%= name %>"><%= name %></li> #设置title属性 <% }) %> </ul>
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); }); }
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);
连接到分
析对象获取数据:bufferSource.connect(analyser);
实现可视化功能函数:
function visualizer(){ var arr = new Uint8Array(analyser.frequencyBinCount); analyser.getByteFrequencyData(arr); console.log(arr); }
调用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;
利用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!

html5的div元素默认一行不可以放两个。div是一个块级元素,一个元素会独占一行,两个div默认无法在同一行显示;但可以通过给div元素添加“display:inline;”样式,将其转为行内元素,就可以实现多个div在同一行显示了。

html5中列表和表格的区别:1、表格主要是用于显示数据的,而列表主要是用于给数据进行布局;2、表格是使用table标签配合tr、td、th等标签进行定义的,列表是利用li标签配合ol、ul等标签进行定义的。

固定方法:1、使用header标签定义文档头部内容,并添加“position:fixed;top:0;”样式让其固定不动;2、使用footer标签定义尾部内容,并添加“position: fixed;bottom: 0;”样式让其固定不动。

HTML5中画布标签是“<canvas>”。canvas标签用于图形的绘制,它只是一个矩形的图形容器,绘制图形必须通过脚本(通常是JavaScript)来完成;开发者可利用多种js方法来在canvas中绘制路径、盒、圆、字符以及添加图像等。

html5中不支持的标签有:1、acronym,用于定义首字母缩写,可用abbr替代;2、basefont,可利用css样式替代;3、applet,可用object替代;4、dir,定义目录列表,可用ul替代;5、big,定义大号文本等等。

html5废弃了dir列表标签。dir标签被用来定义目录列表,一般和li标签配合使用,在dir标签对中通过li标签来设置列表项,语法“<dir><li>列表项值</li>...</dir>”。HTML5已经不支持dir,可使用ul标签取代。

html5是指超文本标记语言(HTML)的第五次重大修改,即第5代HTML。HTML5是Web中核心语言HTML的规范,用户使用任何手段进行网页浏览时看到的内容原本都是HTML格式的,在浏览器中通过一些技术处理将其转换成为了可识别的信息。HTML5由不同的技术构成,其在互联网中得到了非常广泛的应用,提供更多增强网络应用的标准机。

因为html5不基于SGML(标准通用置标语言),不需要对DTD进行引用,但是需要doctype来规范浏览器的行为,也即按照正常的方式来运行,因此html5只需要写doctype即可。“!DOCTYPE”是一种标准通用标记语言的文档类型声明,用于告诉浏览器编写页面所用的标记的版本。


Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

Safe Exam Browser
Safe Exam Browser is a secure browser environment for taking online exams securely. This software turns any computer into a secure workstation. It controls access to any utility and prevents students from using unauthorized resources.

ZendStudio 13.5.1 Mac
Powerful PHP integrated development environment

SublimeText3 English version
Recommended: Win version, supports code prompts!

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools
