bugfix transposing
This commit is contained in:
@@ -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],
|
||||
|
||||
@@ -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'},
|
||||
]);
|
||||
});
|
||||
|
||||
|
||||
@@ -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);
|
||||
}
|
||||
|
||||
@@ -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');
|
||||
}
|
||||
});
|
||||
});
|
||||
|
||||
@@ -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;
|
||||
}
|
||||
|
||||
|
||||
@@ -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'}]);
|
||||
}));
|
||||
});
|
||||
|
||||
@@ -21,12 +21,13 @@ export class SongTextComponent implements OnInit {
|
||||
@Input() public index = -1;
|
||||
@Input() public fullscreen = false;
|
||||
@Input() public showSwitch = false;
|
||||
@Input() public transpose: TransposeMode | null = null;
|
||||
@Output() public chordModeChanged = new EventEmitter<ChordMode>();
|
||||
@ViewChildren('section') public viewSections: QueryList<ElementRef<HTMLElement>> | null = null;
|
||||
public faLines = faGripLines;
|
||||
public offset = 0;
|
||||
public iChordMode: ChordMode = 'hide';
|
||||
private iText = '';
|
||||
private iTranspose: TransposeMode | null = null;
|
||||
|
||||
public constructor(private textRenderingService: TextRenderingService, private elRef: ElementRef<HTMLElement>) {}
|
||||
|
||||
@@ -37,15 +38,29 @@ export class SongTextComponent implements OnInit {
|
||||
|
||||
@Input()
|
||||
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.sections = [];
|
||||
if (this.fullscreen) {
|
||||
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
|
||||
);
|
||||
} 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);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user