Skip to content

Instantly share code, notes, and snippets.

@Python1320
Created July 10, 2022 14:25
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Python1320/f4b514d269c2cf75454deddd202a9ead to your computer and use it in GitHub Desktop.
Save Python1320/f4b514d269c2cf75454deddd202a9ead to your computer and use it in GitHub Desktop.
GMod youtube fft test (old but seems to work still)
local asdd = vgui.Create'DHTML'
timer.Simple(20,function() asdd:Remove() end)
asdd:OpenURL"https://www.youtube.com/watch?v=zFcY5xO4dGA"
asdd:SetSize(600,600)
asdd:SetAlpha(255)
local fft={}
asdd.PaintOver=function(self,w,h)
local max=#fft + 1
local width = w/max
surface.SetDrawColor(255,33,33,222)
surface.DrawRect(0,0,32,32)
for k,v in next,fft do
local H=v*2
surface.DrawRect((k-1)*width,h-H,width,H)
end
--return DHTML.Paint(self,w,h)
end
timer.Simple(5,function()
asdd:AddFunction("gmod","fft",function(d)
local data = d:fromhex()
fft = {data:byte(1,-1)}
end)
asdd:RunJavascript[['use strict';
document.cookie="PREF=f1=50000000&f2=40000000&f4=20000&f5=30; wide=1";
document.cookie="CONSENT=YES+SE.fi+V11+BX";
var doDebug = true;
var audioCtx = null;
var analyser = null;
var dataArray = null;
var source = null;
var $video = null;
init();
setElementSource("video");
requestAnimationFrame(animate);
//Init function
function init() {
try {
//Get audio apis from different browsers
if (!(navigator.getUserMedia)) {
navigator.getUserMedia = (navigator.getUserMedia ||
navigator.webkitGetUserMedia ||
navigator.mozGetUserMedia ||
navigator.msGetUserMedia);
}
if (!(window.AudioContext)) {
window.AudioContext = window.AudioContext || window.webkitAudioContext;
}
//Create the audio context
if (!(audioCtx)) {
audioCtx = new AudioContext();
}
//Setup the analyser node
if (!(analyser)) {
analyser = audioCtx.createAnalyser();
analyser.fftSize = 256;
analyser.minDecibels = -80;
analyser.maxDecibels = 0;
analyser.smoothingTimeConstant = 0.8;
}
//Generate the dataArray
if (!(dataArray)) {
dataArray = new Uint8Array(analyser.fftSize / 2);
}
//Simple function to map values from one range to another
Number.prototype.map = function(in_min, in_max, out_min, out_max) {
return (this - in_min) * (out_max - out_min) / (in_max - in_min) + out_min;
};
debug("Init successfull!", "INFO");
} catch (e) {
alert("Error! Probably your browser doesn't support the Web Audio API!");
debug(e, "ERROR");
}
}
//Try to find the video DOM with given ID and create an audio source
function setElementSource(id) {
//Try to find the video
$video = document.querySelectorAll("video")[0];
if ($video) {
//Create audio element
source = audioCtx.createMediaElementSource($video);
//Route source to analyser & speakers
source.connect(analyser);
source.connect(audioCtx.destination);
debug("setElementSource successfull!", "INFO");
} else {
debug("The video element was not found!", "WARNING");
}
}
function arrToHex(ag) {
var s = '', h = '0123456789ABCDEF';
ag.forEach((v) => { s += h[v >> 4] + h[v & 15]; });
return s;
}
//The animate loop
function animate() {
//Animate loop
requestAnimationFrame(animate);
//Get the audio data
passByteFrequencyData(dataArray);
gmod.fft(""+arrToHex(dataArray));
}
/************
UTILS
************/
//A debug function
function debug(msg, type) {
if (doDebug) {
switch (type) {
case "ERROR":
console.log("[ERROR] YTMV > " + msg);
break;
case "INFO":
console.log("[INFO] YTMV > " + msg);
break;
case "WARNING":
console.log("[WARNING] YTMV > " + msg);
break;
default:
console.log("[DEBUG] YTMV > " + msg);
break;
}
}
}
//Get the data from the running analyser
function passByteFrequencyData(array) {
try {
analyser.getByteFrequencyData(array);
} catch (e) {
debug("Error passing the ByteFrequencyData!", "ERROR");
}
}
]]
end)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment