forked from tidalcycles/strudel
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge branch 'tidalcycles:main' into main
- Loading branch information
Showing
9 changed files
with
157 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,69 @@ | ||
import { Pattern, clamp } from '@strudel/core'; | ||
import { getDrawContext, getTheme } from '@strudel/draw'; | ||
import { analysers, getAnalyzerData } from 'superdough'; | ||
|
||
/** | ||
* Renders a spectrum analyzer for the incoming audio signal. | ||
* @name spectrum | ||
* @param {object} config optional config with options: | ||
* @param {integer} thickness line thickness in px (default 3) | ||
* @param {integer} speed scroll speed (default 1) | ||
* @param {integer} min min db (default -80) | ||
* @param {integer} max max db (default 0) | ||
* @example | ||
* n("<0 4 <2 3> 1>*3") | ||
* .off(1/8, add(n(5))) | ||
* .off(1/5, add(n(7))) | ||
* .scale("d3:minor:pentatonic") | ||
* .s('sine') | ||
* .dec(.3).room(.5) | ||
* ._spectrum() | ||
*/ | ||
let latestColor = {}; | ||
Pattern.prototype.spectrum = function (config = {}) { | ||
let id = config.id ?? 1; | ||
return this.analyze(id).draw( | ||
(haps) => { | ||
config.color = haps[0]?.value?.color || latestColor[id] || getTheme().foreground; | ||
latestColor[id] = config.color; | ||
drawSpectrum(analysers[id], config); | ||
}, | ||
{ id }, | ||
); | ||
}; | ||
|
||
Pattern.prototype.scope = Pattern.prototype.tscope; | ||
|
||
const lastFrames = new Map(); | ||
|
||
function drawSpectrum( | ||
analyser, | ||
{ thickness = 3, speed = 1, min = -80, max = 0, ctx = getDrawContext(), id = 1, color } = {}, | ||
) { | ||
ctx.lineWidth = thickness; | ||
ctx.strokeStyle = color; | ||
|
||
if (!analyser) { | ||
// if analyser is undefined, draw straight line | ||
// it may be undefined when no sound has been played yet | ||
return; | ||
} | ||
const scrollSize = speed; | ||
const dataArray = getAnalyzerData('frequency', id); | ||
const canvas = ctx.canvas; | ||
ctx.fillStyle = color; | ||
const bufferSize = analyser.frequencyBinCount; | ||
let imageData = lastFrames.get(id) || ctx.getImageData(0, 0, canvas.width, canvas.height); | ||
lastFrames.set(id, imageData); | ||
ctx.clearRect(0, 0, ctx.canvas.width, ctx.canvas.height); | ||
ctx.putImageData(imageData, -scrollSize, 0); | ||
let q = canvas.width - speed; | ||
for (let i = 0; i < bufferSize; i++) { | ||
const normalized = clamp((dataArray[i] - min) / (max - min), 0, 1); | ||
ctx.globalAlpha = normalized; | ||
const next = (Math.log(i + 1) / Math.log(bufferSize)) * canvas.height; | ||
const size = 2; //next - pos; | ||
ctx.fillRect(q, canvas.height - next, scrollSize, size); | ||
} | ||
lastFrames.set(id, ctx.getImageData(0, 0, canvas.width, canvas.height)); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters