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],
'C#': [KEYS_MAJOR_FLAT, KEYS_MINOR_FLAT], 'C#': [KEYS_MAJOR_FLAT, KEYS_MINOR_FLAT],
Db: [KEYS_MAJOR_B, KEYS_MINOR_B], 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], 'D#': [KEYS_MAJOR_FLAT, KEYS_MINOR_FLAT],
Eb: [KEYS_MAJOR_B, KEYS_MINOR_B], Eb: [KEYS_MAJOR_B, KEYS_MINOR_B],
E: [KEYS_MAJOR_FLAT, KEYS_MINOR_FLAT], 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 // c c# db c7 cmaj7 c/e
void expect(sections[2].lines[0].chords).toEqual([ void expect(sections[2].lines[0].chords).toEqual([
{chord: 'c', length: 1, position: 0}, {chord: 'c', length: 1, position: 0, add: null, slashChord: null},
{chord: 'c#', length: 2, position: 2}, {chord: 'c#', length: 2, position: 2, add: null, slashChord: null},
{chord: 'db', length: 2, position: 5}, {chord: 'db', length: 2, position: 5, add: null, slashChord: null},
{chord: 'c', length: 2, position: 8, add: '7'}, {chord: 'c', length: 2, position: 8, add: '7', slashChord: null},
{chord: 'c', length: 5, position: 13, add: 'maj7'}, {chord: 'c', length: 5, position: 13, add: 'maj7', slashChord: null},
{chord: 'c', length: 3, position: 22, slashChord: 'e'}, {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 { private getLineOfLineText(text: string, transpose: TransposeMode | null): Line | null {
if (!text) { if (!text) return null;
return null;
}
const cords = this.readChords(text); const cords = this.readChords(text);
const hasMatches = cords.length > 0; const hasMatches = cords.length > 0;
const type = hasMatches ? LineType.chord : LineType.text; const type = hasMatches ? LineType.chord : LineType.text;
const line: Line = {type, text, chords: hasMatches ? cords : null}; 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.transpose(line, transpose.baseKey, transpose.targetKey)
: this.transposeService.renderChords(line); : this.transposeService.renderChords(line);
} }

View File

@@ -10,11 +10,21 @@ describe('TransposeService', () => {
service = TestBed.inject(TransposeService); service = TestBed.inject(TransposeService);
}); });
it('should create map', () => { it('should create map upwards', () => {
const distance = service.getDistance('D', 'G'); const distance = service.getDistance('D', 'G');
const map = service.getMap('D', distance); const map = service.getMap('D', distance);
console.log(map); if (map) {
void expect(service).toBeTruthy(); 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 { export class TransposeService {
public transpose(line: Line, baseKey: string, targetKey: string): Line { public transpose(line: Line, baseKey: string, targetKey: string): Line {
if (line.type !== LineType.chord) { if (line.type !== LineType.chord || !line.chords) return line;
return line;
}
const difference = this.getDistance(baseKey, targetKey); const difference = this.getDistance(baseKey, targetKey);
const map = this.getMap(baseKey, difference); const map = this.getMap(baseKey, difference);
const chords = const chords = difference !== 0 && map ? line.chords.map(chord => this.transposeChord(chord, map)) : line.chords;
difference > 0 && line.chords && map ? line.chords.map(chord => this.transposeChord(chord, map)) : line.chords; const renderedLine = this.renderLine(chords);
const renderedLine = this.renderLine(chords ?? []);
return {...line, text: renderedLine, chords}; return {...line, text: renderedLine, chords};
} }
@@ -49,15 +47,19 @@ export class TransposeService {
const map: {[key: string]: string} = {}; const map: {[key: string]: string} = {};
for (let i = 0; i < 12; i++) { for (let i = 0; i < 12; i++) {
const source = scale[0][i]; const source = scale[0][i];
const mappedIndex = (i + difference) % 12; const mappedIndex = (i + difference + 12) % 12;
map[source] = scale[0][mappedIndex]; const mapped = scale[0][mappedIndex];
console.log(mapped);
map[source] = mapped;
} }
for (let i = 0; i < 12; i++) { for (let i = 0; i < 12; i++) {
const source = scale[1][i]; const source = scale[1][i];
const mappedIndex = (i + difference) % 12; const mappedIndex = (i + difference + 12) % 12;
map[source] = scale[1][mappedIndex]; const mapped = scale[1][mappedIndex];
console.log(mapped);
map[source] = mapped;
} }
console.log(map);
return 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 {SongListComponent} from './song-list.component';
import {of} from 'rxjs'; import {of} from 'rxjs';
@@ -34,9 +34,4 @@ describe('SongListComponent', () => {
it('should create', () => { it('should create', () => {
void expect(component).toBeTruthy(); void expect(component).toBeTruthy();
}); });
it('should read songs from SongService', fakeAsync(() => {
tick();
void expect(component.songs$).toEqual([{title: 'title1'}]);
}));
}); });

View File

@@ -21,12 +21,13 @@ export class SongTextComponent implements OnInit {
@Input() public index = -1; @Input() public index = -1;
@Input() public fullscreen = false; @Input() public fullscreen = false;
@Input() public showSwitch = false; @Input() public showSwitch = false;
@Input() public transpose: TransposeMode | null = null;
@Output() public chordModeChanged = new EventEmitter<ChordMode>(); @Output() public chordModeChanged = new EventEmitter<ChordMode>();
@ViewChildren('section') public viewSections: QueryList<ElementRef<HTMLElement>> | null = null; @ViewChildren('section') public viewSections: QueryList<ElementRef<HTMLElement>> | null = null;
public faLines = faGripLines; public faLines = faGripLines;
public offset = 0; public offset = 0;
public iChordMode: ChordMode = 'hide'; public iChordMode: ChordMode = 'hide';
private iText = '';
private iTranspose: TransposeMode | null = null;
public constructor(private textRenderingService: TextRenderingService, private elRef: ElementRef<HTMLElement>) {} public constructor(private textRenderingService: TextRenderingService, private elRef: ElementRef<HTMLElement>) {}
@@ -37,15 +38,29 @@ export class SongTextComponent implements OnInit {
@Input() @Input()
public set text(value: string) { public set text(value: string) {
this.sections = []; this.iText = value;
this.render();
}
@Input()
public set transpose(value: TransposeMode | null) {
this.iTranspose = value;
this.render();
}
private render() {
this.offset = 0; this.offset = 0;
this.sections = [];
if (this.fullscreen) { if (this.fullscreen) {
setTimeout( setTimeout(
() => (this.sections = this.textRenderingService.parse(value, this.transpose).sort((a, b) => a.type - b.type)), () =>
(this.sections = this.textRenderingService
.parse(this.iText, this.iTranspose)
.sort((a, b) => a.type - b.type)),
100 100
); );
} else { } else {
this.sections = this.textRenderingService.parse(value, this.transpose).sort((a, b) => a.type - b.type); this.sections = this.textRenderingService.parse(this.iText, this.iTranspose).sort((a, b) => a.type - b.type);
} }
} }