Skip to content

Commit

Permalink
Merge pull request #51 from dxinteractive/feature/ruler2
Browse files Browse the repository at this point in the history
Pitch ruler
  • Loading branch information
dxinteractive authored Jul 15, 2021
2 parents cf8e61f + 4e94643 commit 559cf8f
Show file tree
Hide file tree
Showing 12 changed files with 521 additions and 219 deletions.
25 changes: 24 additions & 1 deletion packages/mosc/src/__tests__/mosc.test.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import {centsToRatio, octaveDivisionToRatio, sortByTime, scoreToMs} from '../mosc';
import {centsToRatio, octaveDivisionToRatio, ratioToCents, ratioToOctaveDivision, sortByTime, scoreToMs} from '../mosc';

describe('centsToRatio', () => {
it('should convert cent to ratio', () => {
Expand All @@ -24,6 +24,29 @@ describe('octaveDivisionToRatio', () => {
});
});

describe('ratioToCents', () => {
it('should convert cent to ratio', () => {
expect(ratioToCents(1)).toBe(0);
expect(ratioToCents(0.5)).toBe(-1200);
expect(ratioToCents(2)).toBe(1200);
expect(ratioToCents(4)).toBe(2400);
});

it('should convert cent to ratio with octave', () => {
expect(ratioToCents(2, 1)).toBe(0);
expect(ratioToCents(4, 2)).toBe(0);
});
});

describe('ratioToOctaveDivision', () => {
it('should ratio to octave division', () => {
expect(ratioToOctaveDivision(1.2599210498948732, 12, 2)).toBe(4);
expect(ratioToOctaveDivision(2, 6, 2)).toBe(6);
expect(ratioToOctaveDivision(4, 6, 2)).toBe(12);
expect(ratioToOctaveDivision(4, 6, 2, 2)).toBe(0);
});
});

describe('sortByTime', () => {
it('should sort items by time', () => {
expect(sortByTime([
Expand Down
8 changes: 8 additions & 0 deletions packages/mosc/src/mosc.ts
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,14 @@ export const octaveDivisionToRatio = (steps: number, stepsInOctave: number, octa
return Math.pow(octaveSize, (steps + (octave * stepsInOctave)) / stepsInOctave);
};

export const ratioToCents = (ratio: number, octave: number = 0): number => {
return ratioToOctaveDivision(ratio, 1200, 2, octave);
};

export const ratioToOctaveDivision = (ratio: number, stepsInOctave: number, octaveSize: number, octave: number = 0): number => {
return (Math.log(ratio) / Math.log(octaveSize) * stepsInOctave) - (octave * stepsInOctave);
};

export const sortByTime = <T extends {time: number}>(items: T[]): T[] => {
return items.slice().sort((a, b) => {
if(a.time < b.time) return -1;
Expand Down
2 changes: 2 additions & 0 deletions packages/xenpaper-ui/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
"@xenpaper/sound-engine-tonejs": "0.0.1",
"dendriform": "^2.0.0-alpha.8",
"konva": "^8.1.0",
"hsl-to-hex": "1.0.0",
"rd-parse": "^3.2.3",
"react-copy-to-clipboard": "^5.0.3",
"react-helmet": "^6.1.0",
Expand All @@ -46,6 +47,7 @@
"@testing-library/jest-dom": "^5.11.6",
"@testing-library/react": "^11.2.2",
"@testing-library/user-event": "^12.3.0",
"@types/hsl-to-hex": "1.0.0",
"@types/jest": "^26.0.16",
"@types/node": "^12.19.8",
"@types/react": "^16.14.2",
Expand Down
Loading

0 comments on commit 559cf8f

Please sign in to comment.