小薇 - 黄品源[音乐频谱外链]
本帖最后由 亚伦影音工作室 于 2024-12-22 18:57 编辑 <br /><br /><style>#papa {margin: 150px -300px;width: 1164px;height: 620px;background: url('https://pic.imgdb.cn/item/65b76cfb871b83018ab9b60e.png') no-repeat 90% 50%/100px 200px,url('https://pic.imgdb.cn/item/65b76f2c871b83018ac00c9e.png') no-repeat 80% 35%/220px 300px, url('https://pic.imgdb.cn/item/64e0d36a661c6c8e541a39a5.jpg') no-repeat center/cover; z-index: 1;display: grid; overflow: hidden;position: relative;}
#aud {position:absolute;margin-top:0px; margin-left:0px;z-index: 60;
display: block;
width:102%;clip-path: inset(20% 1% 20% 1%);filter:invert(100%);margin: 580px -14px;
}
#lrc {
--state: paused;
--motion: cover2;
--tt: 2s;
--bg: linear-gradient(0deg, #ff0000, #ff0000, #ff0000);
position: absolute;z-index: 6;
left: 52%;
transform: translate(-50%);
top: 480px;
font:normal 3em 华文新魏;
font-weight:400;
color: #eeeeee;
white-space: pre;
-webkit-background-clip: text;
filter:drop-shadow(#000 1px 0 0)drop-shadow(#000 0 1px 0)drop-shadow(#000 -1px 0 0) drop-shadow(#000 0 -1px0);
}
#lrc::before {
position: absolute;
content: attr(data-lrc);
width: 20%;
height: 100%;
color: transparent;
overflow: hidden;
white-space: pre;
background: var(--bg);
-webkit-background-clip: text;
animation: var(--motion) var(--tt) linear forwards;
animation-play-state: var(--state);
}
@keyframes cover1{0% { width: 0%;}100% { width: 100%;}}
@keyframes cover2 { 0% { width: 0%;}100% { width: 100%;}}
#canvas{position: absolute;width: 55%;z-index: 4;top:340px; left:250px; }
</style>
<div id="papa">
<audio id="aud"src="https://s2.ananas.chaoxing.com/sv-w8/audio/e6/20/2a/da37d85940ed4d18cd8a6ad814754d2e/audio.mp3" autoplay loopcontrols crossOrigin="anonymous"></audio>
<div data-lrc="亚伦影音工作室" id="lrc" title="歌词显示">亚伦影音工作室</div>
<canvas id="canvas" "></canvas>
</div>
<script >
(function() {
/*原始lrc歌词*/
let lrcStr = `小薇 - 黄品源
词:阿弟
曲:阿弟
有一个美丽的小女孩
她的名字叫作小薇
她有双温柔的眼睛
她悄悄偷走我的心
小薇啊
你可知道我多爱你
我要带你飞到天上去
看那星星多美丽
摘下一颗亲手送给你
有一个美丽的小女孩
她的名字叫作小薇
她有双温柔的眼睛
她悄悄偷走我的心
小薇啊
你可知道我多爱你
我要带你飞到天上去
看那星星多美丽
摘下一颗亲手送给你
小薇啊
你可知道我多爱你
我要带你飞到天上去
看那星星多美丽
摘下一颗亲手送给你
`;
/*变量 :mKey - 当前歌词索引;mFlag :调用关键帧动画索引;averAdd :平均值补偿*/
let mKey = 0, mFlag = true, averAdd = 0.3;
/*函数 :获取每句歌词用时,歌词用时若超过平均值则取平均值,最后一句歌词则取平均值*/
let lrcTime = (ar) => {
let tmpAr = [];
for(j = 0; j <ar.length - 1; j ++) {
if(j !== ar.length - 1) tmpAr = parseFloat((ar - ar).toFixed(1));
}
let aver = parseInt(tmpAr.reduce((a,b) => a + b) / (tmpAr.length - 1)) + averAdd;
tmpAr.push(aver);
tmpAr.forEach((item,key) => {
ar = item > aver ? aver : item;
});
return ar;
};
/*函数 :从原始lrc歌词获取信息并存入 n*3 数组*/
let getLrcAr = (text) => {
let lrcAr = [];
let calcRule = ;
for(x of text.split('\n')) {
let ar = [];
let re = /\d+[\.:]\d+([\.:]\d+)?/g;
let geci = x.replace(re,'');
if(geci) {
geci = geci.replace(/[\[\]\'\"\t,]s?/g,'');
let time = x.match(re);
if(time != null) {
for(y of time) {
let tmp = y.match(/\d+/g);
let sec = 0;
for(z in tmp) sec += tmp * calcRule;
ar = ;
lrcAr.push(ar);
}
}
}
}
lrcAr.sort((a,b)=> a - b);
return(lrcTime(lrcAr));
};
/*函数 :模拟显示同步歌词*/
let showLrc = (time) => {
let name = mFlag ? 'cover1' : 'cover2';
lrc.innerHTML = lrcAr;
lrc.dataset.lrc = lrcAr;
lrc.style.setProperty('--motion', name);
lrc.style.setProperty('--tt', time + 's');
lrc.style.setProperty('--state', 'running');
mKey += 1;
mFlag = !mFlag;
};
/*函数 :处理当前歌词索引 mKey*/
let calcKey = () => {
for (j = 0; j < lrcAr.length; j++) {
if (aud.currentTime <= lrcAr) {
mKey = j - 1;
break;
}
}
if (mKey < 0) mKey = 0;
if (mKey > lrcAr.length - 1) mKey = lrcAr.length - 1;
let time = lrcAr - (aud.currentTime - lrcAr);
showLrc(time);
};
/*格式化时间信息*/
let toMin = (val) => {
if (!val) return '00:00';
val = Math.floor(val);
let min = parseInt(val / 60),
sec = parseFloat(val % 60);
if (min < 10) min = '0' + min;
if (sec < 10) sec = '0' + sec;
return min + ':' + sec;
}
/*函数 :关键帧动画状态切换*/
let mState = () => aud.paused ? (lrc.style.setProperty('--state','paused'),mplayer.style.animationPlayState = 'paused') : (lrc.style.setProperty('--state','running'),mplayer.style.animationPlayState = 'running');
/*监听播放进度*/
aud.addEventListener('timeupdate', () => {
for (j = 0; j < lrcAr.length; j++) {
if (aud.currentTime >= lrcAr) {
cKey = j;
if (mKey === j) showLrc(lrcAr);
else continue;
}
}
});
aud.addEventListener('pause', () => mState());/*监听暂停事件*/
aud.addEventListener('play', () => mState());/*监听播放事件*/
aud.addEventListener('seeked', () => calcKey());/*监听查询事件*/
let lrcAr = getLrcAr(lrcStr); /*获得歌词数组*/
})();
</script>
<script>
window.onload = function () {
var oAudio = document.getElementById('aud');
// 创建音频上下文对象
var oCtx = new AudioContext();
// console.log(oCtx);
// 创建媒体源,除了audio本身可以获取,也可以通过oCtx对象提供的api进行媒体源操作
var audioSrc = oCtx.createMediaElementSource(oAudio);
// 创建分析机
var analyser = oCtx.createAnalyser();
// 媒体源与分析机连接
audioSrc.connect(analyser);
// 输出的目标:将分析机分析出来的处理结果与目标点(耳机/扬声器)连接
analyser.connect(oCtx.destination);
// 利用cancas渐变进行音频绘制
var ctx = canvas.getContext('2d');
canvas.width = window.innerWidth;
canvas.height = window.innerHeight;
var oW = canvas.width;
var oH = canvas.height;
var color1 = ctx.createLinearGradient(oW / 2, oH / 2 - 30, oW / 2, oH / 2 - 100);
var color2 = ctx.createLinearGradient(oW / 2, oH / 2 + 30, oW / 2, oH / 2 + 100);
color1.addColorStop(0, '#ffffff');
color1.addColorStop(.5, '#fff000');
color1.addColorStop(1, '#00ff00');
color2.addColorStop(0, '#ffffff');
color2.addColorStop(.5, '#fff000');
color2.addColorStop(1, '#00ff00');
// 音频图的条数
var count = 200;
// 缓冲区:进行数据的缓冲处理,转换成二进制数据
var voiceHeight = new Uint8Array(analyser.frequencyBinCount);
// console.log(voiceHeight);
function draw() {
// 将当前的频率数据复制到传入的无符号字节数组中,做到实时连接
analyser.getByteFrequencyData(voiceHeight);
// console.log(voiceHeight);
// 自定义获取数组里边数据的频步
var step = Math.round(voiceHeight.length / count);
ctx.clearRect(0, 0, oW, oH);
for (var i = 0; i < count; i++) {
var audioHeight = voiceHeight;
ctx.fillStyle = color1;// 绘制向上的线条
ctx.fillRect(oW / 2 + (i * 5), oH / 2, 2, -audioHeight);
ctx.fillRect(oW / 2 - (i * 5), oH / 2, 2, -audioHeight);
ctx.fillStyle = color2;// 绘制向下的线条
ctx.fillRect(oW / 2 + (i * 5), oH / 2, 2, audioHeight);
ctx.fillRect(oW / 2 - (i * 5), oH / 2, 2, audioHeight);
}
window.requestAnimationFrame(draw);
}
draw();
}
</script>
漂亮的制作{:4_187:} 欣赏佳作 感谢分享 漂亮!欣赏学习!{:4_180:} 漂亮的制作。欣赏亚伦老师好帖{:4_199:}
页:
[1]