Skip to content

Commit

Permalink
fix(tools): mapStartToEvent is curried
Browse files Browse the repository at this point in the history
  • Loading branch information
ricardomatias committed Feb 19, 2021
1 parent 8f78b17 commit 8b04cf7
Show file tree
Hide file tree
Showing 10 changed files with 138 additions and 59 deletions.
1 change: 0 additions & 1 deletion lib/composition/arp.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,6 @@ export function createArp<T extends Scale | Chord>(

const eventsCount = Math.max(melody.length, rhythm.length);


if (isEvent(rhythm)) {
rhythm = R.compose(mapDurations, convertEventsToNotevalues)(rhythm);
} else {
Expand Down
4 changes: 2 additions & 2 deletions lib/composition/chord-progression.ts
Original file line number Diff line number Diff line change
Expand Up @@ -203,12 +203,12 @@ export function createChordProgression(
pattern = rhythm.map((event) => {
if (event.isRest) {
return ChordEvent({
...mapStartToEvent(event, time),
...mapStartToEvent(time, event),
});
}

return ChordEvent({
...mapStartToEvent(event, time),
...mapStartToEvent(time, event),
chord: chordNotes,
chordName: chord.name,
});
Expand Down
4 changes: 2 additions & 2 deletions lib/composition/motif.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,11 +29,11 @@ export function createMotif(notes: Note[], rhythm: string[] | Event[], startTime
const note = choose(notes);

if (event.isRest) {
return NoteEvent(mapStartToEvent(event, start));
return NoteEvent(mapStartToEvent(start, event));
}

return NoteEvent({
...mapStartToEvent(event, start),
...mapStartToEvent(start, event),
note: note.n,
midi: note.m,
});
Expand Down
2 changes: 1 addition & 1 deletion lib/composition/movement/default.ts
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,7 @@ function movement(key: Key, length: TimeFormat, turns: number, {
dur: 0,
};
}
}).filter(R.has('time'));
}).filter((evt) => R.has('time', evt));
} else if (rhythm === MovementRhythm.Turn) {
rhythmEvents = <TimelineEvent[]>Rhythm.turn(time, turns, {
minNoteValue: 8,
Expand Down
52 changes: 26 additions & 26 deletions lib/composition/movement/turn.ts
Original file line number Diff line number Diff line change
Expand Up @@ -131,33 +131,33 @@ function movement(key: Key, turns: Turn[] = DEFAULT_TURNS, length: TimeFormat, {
}

switch (turnType) {
case TurnMoves.Start:
case TurnMoves.Start:
// create a new key from the starting key
key = new Key(startingKey.root, startingKey.type);
mainKeyRoot = key.root;

key.modulateMode({ interval: interval as number });
break;
case TurnMoves.Keep:
break;
case TurnMoves.ModeUp:
key.modulateMode({ direction: Key.ModulateUp, interval: interval as number });

break;
case TurnMoves.ModeDown:
key.modulateMode({ direction: Key.ModulateDown, interval: interval as number });

break;
case TurnMoves.ModulateUp:
event.key = modulate(key, events, Key.ModulateUp, { type: turnType, interval: interval as Interval });

mainKeyRoot = key.root;
break;
case TurnMoves.ModulateDown:
event.key = modulate(key, events, Key.ModulateDown, { type: turnType, interval: interval as Interval });

mainKeyRoot = key.root;
break;
key = new Key(startingKey.root, startingKey.type);
mainKeyRoot = key.root;

key.modulateMode({ interval: interval as number });
break;
case TurnMoves.Keep:
break;
case TurnMoves.ModeUp:
key.modulateMode({ direction: Key.ModulateUp, interval: interval as number });

break;
case TurnMoves.ModeDown:
key.modulateMode({ direction: Key.ModulateDown, interval: interval as number });

break;
case TurnMoves.ModulateUp:
event.key = modulate(key, events, Key.ModulateUp, { type: turnType, interval: interval as Interval });

mainKeyRoot = key.root;
break;
case TurnMoves.ModulateDown:
event.key = modulate(key, events, Key.ModulateDown, { type: turnType, interval: interval as Interval });

mainKeyRoot = key.root;
break;
}

if (isTypeMod(turnType)) {
Expand Down
42 changes: 21 additions & 21 deletions lib/composition/sequence.ts
Original file line number Diff line number Diff line change
Expand Up @@ -159,27 +159,27 @@ export class Sequence {
let chord: Chord | undefined;

switch (romanPos) {
case 'I':
chord = scale.I?.chord;
break;
case 'II':
chord = scale.II?.chord;
break;
case 'III':
chord = scale.III?.chord;
break;
case 'IV':
chord = scale.IV?.chord;
break;
case 'V':
chord = scale.V?.chord;
break;
case 'VI':
chord = scale.VI?.chord;
break;
case 'VII':
chord = scale.VII?.chord;
break;
case 'I':
chord = scale.I?.chord;
break;
case 'II':
chord = scale.II?.chord;
break;
case 'III':
chord = scale.III?.chord;
break;
case 'IV':
chord = scale.IV?.chord;
break;
case 'V':
chord = scale.V?.chord;
break;
case 'VI':
chord = scale.VI?.chord;
break;
case 'VII':
chord = scale.VII?.chord;
break;
}

if (chord) this._chords.push(chord);
Expand Down
2 changes: 1 addition & 1 deletion lib/core/Types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,4 +11,4 @@ export interface ChordEvent {
export type CoreClassType = 'scale' | 'chord' | 'key'

export type ChordDescriptor = { root: NoteSymbol, intervals: ChordIntervals | string } &
Partial<Pick<ChordDefinition, 'name' | 'symbol' | 'structure'>>
Partial<Pick<ChordDefinition, 'name' | 'symbol' | 'structure'>>
18 changes: 15 additions & 3 deletions lib/tools/event.ts
Original file line number Diff line number Diff line change
Expand Up @@ -94,19 +94,31 @@ export const computeEventsNext = (events: Event[]): Event[] => {
});
};

export const mapStartToEvent = (event: Event, startTime: TimeFormat): Event => {

/**
* Maps events to a new starting value
*
* @function mapStartToEvent
* @memberof Tools.Event
*
* @param {Event} event
* @param {TimeFormat} startTime
* @return {Event}
*/
export const mapStartToEvent = R.curry((startTime: TimeFormat, event: Event): Event => {
const start = new Time(startTime);
return ({
...event,
time: start.ticks + event.time,
next: start.ticks + event.next,
});
};
});

/**
* Converts Event[] to notevalues (ie. 4n), ignores rests
*
* @function convertEventsToNotevalues
* @memberof Tools.Event
* @description Converts Event[] to notevalues (ie. 4n), ignores rests
*
* @param {Event[]} events
* @return {string[]}
Expand Down
2 changes: 1 addition & 1 deletion lib/tools/midi.ts
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ const findNearestVoice = R.curry((voices, baseVoice): { note: string, midiNote:

/**
* Find the nearest chord
* @function findNearest
* @function findNearestChord
* @memberof Tools.Midi
*
* @param {Array<Number>} baseChord
Expand Down
70 changes: 69 additions & 1 deletion test/tools/event.spec.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { expandDuration } from '../../lib/tools/event';
import * as R from 'ramda';
import { expandDuration, mapStartToEvent } from '../../lib/tools/event';

describe('An Event tools test suite', () => {
describe('#expandDuration', () => {
Expand Down Expand Up @@ -75,4 +76,71 @@ describe('An Event tools test suite', () => {
`);
});
});

describe('#mapStartToEvent', () => {
const events = [
{
dur: 480,
isRest: false,
next: 240,
time: 0,
},
{
dur: 600,
isRest: false,
next: 720,
time: 240,
},
{
dur: 640,
isRest: false,
next: 1360,
time: 720,
},
];

it('should map single', () => {
// when
const mapd = mapStartToEvent(480, events[0]);

// then
expect(mapd).toMatchInlineSnapshot(`
Object {
"dur": 480,
"isRest": false,
"next": 720,
"time": 480,
}
`);
});

it('should map multiple', () => {
// when
const mapd = R.map(mapStartToEvent(480), events);

// then
expect(mapd).toMatchInlineSnapshot(`
Array [
Object {
"dur": 480,
"isRest": false,
"next": 720,
"time": 480,
},
Object {
"dur": 600,
"isRest": false,
"next": 1200,
"time": 720,
},
Object {
"dur": 640,
"isRest": false,
"next": 1840,
"time": 1200,
},
]
`);
});
});
});

0 comments on commit 8b04cf7

Please # to comment.