亚洲精品久久久中文字幕-亚洲精品久久片久久-亚洲精品久久青草-亚洲精品久久婷婷爱久久婷婷-亚洲精品久久午夜香蕉

您的位置:首頁技術文章
文章詳情頁

js通過audioContext實現3D音效

瀏覽:84日期:2024-04-01 18:12:33

本文實例為大家分享了js通過audioContext實現3D音效的具體代碼,供大家參考,具體內容如下

前言

AudioContext的setPosition實現3D音效

效果展示

js通過audioContext實現3D音效

代碼展示

<!DOCTYPE html><html lang='en'><head> <meta charset='UTF-8'> <title>3D Audio</title> <style>body, div{ margin: 0px; padding: 0px; text-align: center;}#cav{ border: 1px solid black; border-radius: 4px; margin: 10px auto;} </style></head><body><canvas height='200'></canvas></body><script> let Aud = function (ctx, url) {this.ctx = ctx;this.url = url;// source節點this.src = ctx.createBufferSource();// 多個處理節點組this.pNode = []; }; Aud.prototype = {output(){ for (let i = 0; i < this.pNode.length; i++){let tNode = this.src;for (let j = 0; j < this.pNode[i].length; j++){ tNode.connect(this.pNode[i][j]); tNode = this.pNode[i][j];}tNode.connect(this.ctx.destination); }},play(loop){ this.src.loop = loop || false; this.output(); this.src.start(0);},stop() { this.src.stop();},addNode(node, groupIdx = 0){ this.pNode[groupIdx] = this.pNode[groupIdx] || []; this.pNode[groupIdx].push(node);} }; //設置節點類型 Aud.NODETYPE = {GNODE: 0 // 表示gainNode節點 } //Aud管理對象 AudManager = {urls: [],items: [],ctx: null,init(){ try{this.ctx = new AudioContext(); }catch (e) {console.log(`${e}`); }},load(callback){ for (let i = 0; i < this.urls.length; i++){this.loadSingle(this.urls[i], callback); }},loadSingle(url, callback){ let req = new XMLHttpRequest(); req.open(’GET’, url, true); req.responseType = ’arraybuffer’; let self = this; req.onload = function () {self.ctx.decodeAudioData(this.response) .then(buf => { let aud = new Aud(self.ctx, url); aud.src.buffer = buf; self.items.push(aud); if (self.items.length == self.urls.length){callback(); }},err => { console.log(`decode error:${err}`);} ) }; req.send();},createNode(nodeType, param){ let node = null; switch (nodeType) {case 1: node = this.ctx.createPanner(); break;case 2: node = this.ctx.createScriptProcessor(param[0], param[1], param[2]); break;default: node = this.ctx.createGain(); } return node;} }; let ctx = document.getElementById(’cav’).getContext(’2d’);// 定義移動點坐標 let cX = 190,cY = 100,deg = 0; window.onload = function (){init(); } function renderCir(x, y, r, col){ctx.save();ctx.beginPath();ctx.arc(x, y, r, 0, Math.PI*2);ctx.closePath();ctx.fillStyle = col;ctx.fill();ctx.restore(); } function renderCenter(){renderCir(160, 100, 8, 'red'); } function renderCat() {renderCir(cX, cY, 8, 'blue'); } function init(){AudManager.urls = ['test.mp3'];AudManager.init();AudManager.load(()=>{ let pNod1 = AudManager.createNode(1); let sound1 = AudManager.items[0]; sound1.addNode(pNod1); sound1.play(true); timeHandle();}); } function timeHandle() {window.setInterval(()=>{ ctx.clearRect(0,0,320,200); let rad = Math.PI*deg / 180; let sx = 90*Math.cos(rad),sy = 90*Math.sin(rad); cX = 160 + sx; cY = 100 + sy; AudManager.items[0].pNode[0][0].setPosition(sx*0.1, -sy*0.1, 0); renderCenter(); renderCat(); deg++;}, 30); }</script></html>

以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持好吧啦網。

標簽: JavaScript
相關文章:
主站蜘蛛池模板: 国产这里有精品 | 野草在线观看视频精品 | 天天综合网天天综合色 | 日本黄大片在线观看视频 | 伊人狠狠丁香婷婷综合色 | 国产精品久久久影院 | 91亚洲国产系列精品第56页 | 精品热线九九精品视频 | qvod激情视频在线观看 | 成人两性高清图片视频 | 一区二区国产精品 | 久久精品91 | 老司机51精品视频在线观看 | 视频成人永久免费视频 | 2021久久精品99精品久久 | 中文偷拍视频在线观看 | 久草视频2 | www.91在线播放 | 国语对白清晰好大好白在线 | 欧美综合偷拍在线另类卡通小说 | 免费簧片在线观看 | 亚洲高速浪潮 | 亚洲精品久久久久久动漫剧情 | 国产精品久久新婚兰兰 | 欧美一级欧美一级在线播放 | 国产一级特黄在线播放 | 亚洲综合色丁香婷婷六月图片 | 国产精品免费视频网站 | 国内精品网站 | 黄色视屏免费观看 | 久久99精品国产麻豆宅宅 | 永久免费的网站 | 7788成年网站免费观看 | 亚洲不卡视频 | 亚洲一区二区黄色 | 免费高清在线影片一区 | 天天鲁天天玩天天爽天天 | 国产亚洲3p一区二区三区 | 久久99精品福利久久久 | 羞羞色院91精品网站 | 天堂亚洲国产日韩在线看 |