bugfix transposing

This commit is contained in:
2021-05-23 13:53:33 +02:00
parent 6c15976f14
commit 3b4dfb5c0e
7 changed files with 56 additions and 35 deletions

View File

@@ -45,7 +45,7 @@ const scaleTypeAssignment: {[key: string]: string[][]} = {
C: [KEYS_MAJOR_FLAT, KEYS_MINOR_FLAT],
'C#': [KEYS_MAJOR_FLAT, KEYS_MINOR_FLAT],
Db: [KEYS_MAJOR_B, KEYS_MINOR_B],
D: [KEYS_MAJOR_B, KEYS_MINOR_B],
D: [KEYS_MAJOR_FLAT, KEYS_MINOR_FLAT],
'D#': [KEYS_MAJOR_FLAT, KEYS_MINOR_FLAT],
Eb: [KEYS_MAJOR_B, KEYS_MINOR_B],
E: [KEYS_MAJOR_FLAT, KEYS_MINOR_FLAT],

View File

@@ -77,12 +77,12 @@ Cool bridge without any chords
// c c# db c7 cmaj7 c/e
void expect(sections[2].lines[0].chords).toEqual([
{chord: 'c', length: 1, position: 0},
{chord: 'c#', length: 2, position: 2},
{chord: 'db', length: 2, position: 5},
{chord: 'c', length: 2, position: 8, add: '7'},
{chord: 'c', length: 5, position: 13, add: 'maj7'},
{chord: 'c', length: 3, position: 22, slashChord: 'e'},
{chord: 'c', length: 1, position: 0, add: null, slashChord: null},
{chord: 'c#', length: 2, position: 2, add: null, slashChord: null},
{chord: 'db', length: 2, position: 5, add: null, slashChord: null},
{chord: 'c', length: 2, position: 8, add: '7', slashChord: null},
{chord: 'c', length: 5, position: 13, add: 'maj7', slashChord: null},
{chord: 'c', length: 3, position: 22, add: null, slashChord: 'e'},
]);
});

View File

@@ -42,15 +42,14 @@ export class TextRenderingService {
}
private getLineOfLineText(text: string, transpose: TransposeMode | null): Line | null {
if (!text) {
return null;
}
if (!text) return null;
const cords = this.readChords(text);
const hasMatches = cords.length > 0;
const type = hasMatches ? LineType.chord : LineType.text;
const line: Line = {type, text, chords: hasMatches ? cords : null};
return transpose
return transpose !== null && transpose !== undefined
? this.transposeService.transpose(line, transpose.baseKey, transpose.targetKey)
: this.transposeService.renderChords(line);
}

View File

@@ -10,11 +10,21 @@ describe('TransposeService', () => {
service = TestBed.inject(TransposeService);
});
it('should create map', () => {
it('should create map upwards', () => {
const distance = service.getDistance('D', 'G');
const map = service.getMap('D', distance);
console.log(map);
void expect(service).toBeTruthy();
if (map) {
void expect(map['D']).toBe('G');
}
});
it('should create map downwards', () => {
const distance = service.getDistance('G', 'D');
const map = service.getMap('G', distance);
if (map) {
void expect(map['G']).toBe('D');
}
});
});

View File

@@ -11,15 +11,13 @@ type TransposeMap = {[key: string]: string};
})
export class TransposeService {
public transpose(line: Line, baseKey: string, targetKey: string): Line {
if (line.type !== LineType.chord) {
return line;
}
if (line.type !== LineType.chord || !line.chords) return line;
const difference = this.getDistance(baseKey, targetKey);
const map = this.getMap(baseKey, difference);
const chords =
difference > 0 && line.chords && map ? line.chords.map(chord => this.transposeChord(chord, map)) : line.chords;
const renderedLine = this.renderLine(chords ?? []);
const chords = difference !== 0 && map ? line.chords.map(chord => this.transposeChord(chord, map)) : line.chords;
const renderedLine = this.renderLine(chords);
return {...line, text: renderedLine, chords};
}
@@ -49,15 +47,19 @@ export class TransposeService {
const map: {[key: string]: string} = {};
for (let i = 0; i < 12; i++) {
const source = scale[0][i];
const mappedIndex = (i + difference) % 12;
map[source] = scale[0][mappedIndex];
const mappedIndex = (i + difference + 12) % 12;
const mapped = scale[0][mappedIndex];
console.log(mapped);
map[source] = mapped;
}
for (let i = 0; i < 12; i++) {
const source = scale[1][i];
const mappedIndex = (i + difference) % 12;
map[source] = scale[1][mappedIndex];
const mappedIndex = (i + difference + 12) % 12;
const mapped = scale[1][mappedIndex];
console.log(mapped);
map[source] = mapped;
}
console.log(map);
return map;
}

View File

@@ -1,4 +1,4 @@
import {ComponentFixture, fakeAsync, TestBed, tick, waitForAsync} from '@angular/core/testing';
import {ComponentFixture, TestBed, waitForAsync} from '@angular/core/testing';
import {SongListComponent} from './song-list.component';
import {of} from 'rxjs';
@@ -34,9 +34,4 @@ describe('SongListComponent', () => {
it('should create', () => {
void expect(component).toBeTruthy();
});
it('should read songs from SongService', fakeAsync(() => {
tick();
void expect(component.songs$).toEqual([{title: 'title1'}]);
}));
});