Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add in FFT example #38

Open
chriskiefer opened this issue Jan 27, 2021 · 1 comment
Open

add in FFT example #38

chriskiefer opened this issue Jan 27, 2021 · 1 comment
Assignees

Comments

@chriskiefer
Copy link

<title> Javascript Audio Processing </title> <script src="https://cdn.jsdelivr.net/gh/google/code-prettify@master/loader/run_prettify.js?autoload=true&skin=sunburst&lang=css" defer="defer"></script> <style type="text/css"> body, button { font-family:Arial, Helvetica, sans-serif; font-size: 16px; } .prettyprint ol.linenums>li { list-style-type: decimal } </style> <script src="maximilian.js"></script> Play <script type="text/javascript"> async function maxi(){ let m = await maximilian(); console.log(m); /** * maxiAudio.init() initialises the Audio Context and should execute in a button click event handler to prevent the console warning * "The AudioContext was not allowed to start. It must be resumed (or created) after a user gesture on the page. https://goo.gl/7K7WLu" */ let playAudio = () => { let myOsc = new m.maxiOsc(); let lfo1 = new m.maxiOsc(); let lfo2 = new m.maxiOsc(); let maxiAudio = new m.maxiAudio(); let dist = new m.maxiNonlinearity(); let fft = new m.maxiFFTAdaptor(); let bins = 512; let hopPercentage = 0.25; fft.setup(bins * 2, Math.floor(bins * 2 * hopPercentage), bins * 2); let mags = fft.getMagnitudesAsJSArray(); let phases = fft.getPhasesAsJSArray(); let numCoeffs = 20; let mfcc = new m.maxiMFCCAdaptor(); mfcc.setup(bins, 40, numCoeffs, 20, 20000); let coeffs = new Float64Array(numCoeffs); maxiAudio.init(); maxiAudio.play = function () { let w = myOsc.saw(50); w = dist.asymclip(w*50, lfo1.sinewave(0.1) * 3, lfo2.coswave(0.02) * 3); if (fft.process(w, m.maxiFFTModes.WITH_POLAR_CONVERSION)) { mags = fft.getMagnitudesAsJSArray(); phases = fft.getPhasesAsJSArray(); console.log(mags); coeffs = mfcc.mfcc(mags); console.log(coeffs); } return w; } } const playButton = document.getElementById('playButton'); playButton.addEventListener("click", () => playAudio()); }; maxi(); </script>
@chriskiefer
Copy link
Author

<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
	<meta http-equiv="content-type" content="text/html; charset=UTF-8">
	<title>
		Javascript Audio Processing
	</title>
	<script src="https://cdn.jsdelivr.net/gh/google/code-prettify@master/loader/run_prettify.js?autoload=true&amp;skin=sunburst&amp;lang=css" defer="defer"></script>
	<!-- show all line numbers-->
	<style type="text/css">
		body, button {
			font-family:Arial, Helvetica, sans-serif;
			font-size: 16px;
		}
		.prettyprint ol.linenums>li {
			list-style-type: decimal
		}
	</style>
	<script src="maximilian.js"></script>
</head>

<body >
	  <button id="playButton">Play</button>
  </div>
	</body>

<!-- Our javascript code -->
<script type="text/javascript">
	async function maxi(){
		let m = await maximilian();
			console.log(m);


			/**
			 * maxiAudio.init() initialises the Audio Context and should execute in a button click event handler to prevent the console warning
			 * "The AudioContext was not allowed to start. It must be resumed (or created) after a user gesture on the page. https://goo.gl/7K7WLu"
			 */
			let playAudio = () => {



				let myOsc = new m.maxiOsc();
				let lfo1 = new m.maxiOsc();
				let lfo2 = new m.maxiOsc();
				let maxiAudio = new m.maxiAudio();
				let dist = new m.maxiNonlinearity();

				let fft = new m.maxiFFTAdaptor();
				let bins = 512;
				let hopPercentage = 0.25;
				fft.setup(bins * 2, Math.floor(bins * 2 * hopPercentage), bins * 2);
				let mags = fft.getMagnitudesAsJSArray();
				let phases = fft.getPhasesAsJSArray();

				let numCoeffs = 20;				
				let mfcc = new m.maxiMFCCAdaptor();
				mfcc.setup(bins, 40, numCoeffs, 20, 20000);
				let coeffs = new Float64Array(numCoeffs);

				maxiAudio.init();

				maxiAudio.play = function () {
					let w = myOsc.saw(50);
					w = dist.asymclip(w*50, lfo1.sinewave(0.1) * 3, lfo2.coswave(0.02) * 3);
					if (fft.process(w, m.maxiFFTModes.WITH_POLAR_CONVERSION)) {
						mags = fft.getMagnitudesAsJSArray();
						phases = fft.getPhasesAsJSArray();
						console.log(mags);
						coeffs = mfcc.mfcc(mags);
						console.log(coeffs);

					}

					return w;
				}
			}

			const playButton = document.getElementById('playButton');
			playButton.addEventListener("click", () => playAudio());

	 };

	 maxi();

</script>
</html>

@chriskiefer chriskiefer self-assigned this Jan 27, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant