Skip to content

Commit

Permalink
Seteable Tempo
Browse files Browse the repository at this point in the history
  • Loading branch information
Pedro Carrazco committed Oct 4, 2018
1 parent 6c39ea5 commit 97e2bb5
Show file tree
Hide file tree
Showing 7 changed files with 45 additions and 25 deletions.
3 changes: 2 additions & 1 deletion src/classes/Scheduler.js
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@ export default class Scheduler {
this.currentTime = timestamp;

this.interval = setInterval(() => {
const rhythmMapObj = rhythmMap()
while (this.currentTime < context.currentTime + this.lookahead) {
// The tick length could be a number or a string that starts
// with 'r', indicating a rest.
Expand All @@ -65,7 +66,7 @@ export default class Scheduler {
if (typeof nextTickLength === 'string' && nextTickLength.charAt(0).toLowerCase() === 'r') {
rest = true;
// Convert it into the appropriate rhythm.
nextTickLength = rhythmMap[nextTickLength.substr(1)];
nextTickLength = rhythmMapObj[nextTickLength.substr(1)];
}
// We're only ticking on beats that aren't rests.
if (!rest) {
Expand Down
2 changes: 1 addition & 1 deletion src/editor.js
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ const existingCode = window.localStorage.getItem('code');
const defaultCode = `# Welcome to Slang! Here's an example to get you started.
# Click the Run button above to start playing this code.
tempo 200
TEMPO 200
# Make a sound called @synth with a triangle wave
@synth (adsr (osc tri) 64n 8n 0.5 8n)
Expand Down
4 changes: 3 additions & 1 deletion src/functions/transpose.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@ export default class Transpose extends FunctionCall {
this.hasWarned = false;
}
next(passedValue) {
const rhythmMapObj = rhythmMap();
console.log(rhythmMapObj);
let nextValue = passedValue || this.data.next();

// Unfortunately transposing rhythms won't work
Expand All @@ -30,7 +32,7 @@ export default class Transpose extends FunctionCall {
typeof nextValue === 'string'
&& (
nextValue.charAt(0).toLowerCase() === 'r'
|| rhythmMap[nextValue]
|| rhythmMapObj[nextValue]
)
) {
if (!this.hasWarned) {
Expand Down
45 changes: 27 additions & 18 deletions src/helpers/parseArguments.js
Original file line number Diff line number Diff line change
@@ -1,23 +1,25 @@
import List from './List';
import FunctionCall from '../functions';

const TEMPO = 120;
const DIVISION = (1 / 24) / (TEMPO / 60);

export const rhythmMap = {
'64t': DIVISION,
'64n': DIVISION * 1.5,
'32t': DIVISION * 2,
'32n': DIVISION * 3,
'16t': DIVISION * 4,
'16n': DIVISION * 6,
'8t': DIVISION * 8,
'8n': DIVISION * 12,
'4t': DIVISION * 16,
'4n':DIVISION * 24,
'2t': DIVISION * 32,
'2n': DIVISION * 48,
'1n': DIVISION * 96,
let TEMPO = 120;

export function rhythmMap () {
const DIVISION = (1 / 24) / (TEMPO / 60);
return {
'64t': DIVISION,
'64n': DIVISION * 1.5,
'32t': DIVISION * 2,
'32n': DIVISION * 3,
'16t': DIVISION * 4,
'16n': DIVISION * 6,
'8t': DIVISION * 8,
'8n': DIVISION * 12,
'4t': DIVISION * 16,
'4n': DIVISION * 24,
'2t': DIVISION * 32,
'2n': DIVISION * 48,
'1n': DIVISION * 96,
};
}

/*
Expand Down Expand Up @@ -52,10 +54,17 @@ export function parseArgument(arg) {

return null;
}
export function changeTempo(tempo) {
let min = 30;
let max = 500;
const newTempo = Math.max(Math.min(tempo, max), min);
TEMPO = isNaN(newTempo) ? TEMPO : newTempo;
}

function createArgumentFromStaticValue(value) {
const rhythmMapObj = rhythmMap()
// convert rhythms into numbers if we catch one
return {
next: () => rhythmMap[value] || value,
next: () => rhythmMapObj[value] || value,
};
}
9 changes: 9 additions & 0 deletions src/runtime.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import Sound from './classes/Sound';
import context from './helpers/context';
import { changeTempo } from './helpers/parseArguments.js';

const model = {
sounds: {},
Expand All @@ -17,6 +18,9 @@ function runScene(scene) {
case 'play':
parsePlay(operation);
break;
case 'tempo':
parseTempo(operation);
break;
}
});

Expand Down Expand Up @@ -55,6 +59,11 @@ function parsePlay(operation) {
model.sounds[operation.sound.name].schedule(operation.patterns);
}

function parseTempo(operation) {
console.log(operation);
changeTempo(operation.value);
}

function clearScene() {
Object.keys(model.sounds).forEach((id) => {
model.sounds[id].destroy();
Expand Down
2 changes: 1 addition & 1 deletion src/slang.js
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,7 @@ semantics.addOperation('toAST', {
Tempo(kw, value) {
return {
type: 'tempo',
value,
value: value.toAST()
};
},

Expand Down
5 changes: 2 additions & 3 deletions src/slang.ohm
Original file line number Diff line number Diff line change
Expand Up @@ -73,8 +73,8 @@ Sound {
the sound beats per minute.
*/

Tempo = TempoKeyword tempoPrim
TempoKeyword = "tempo" | "[]"
Tempo = TempoKeyword int
TempoKeyword = "TEMPO" | "|"

/*
PLAY LINES
Expand Down Expand Up @@ -116,7 +116,6 @@ Sound {
| "-"? digit+ -- int

int = "-"? digit+
tempoPrim = digit+

note = letter "#" digit+ -- sharp
| letter "b" digit+ -- flat
Expand Down

0 comments on commit 97e2bb5

Please sign in to comment.