diff --git a/src/app/modules/songs/services/key.helper.ts b/src/app/modules/songs/services/key.helper.ts index 86264e7..7ea7a7c 100644 --- a/src/app/modules/songs/services/key.helper.ts +++ b/src/app/modules/songs/services/key.helper.ts @@ -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], diff --git a/src/app/modules/songs/services/text-rendering.service.spec.ts b/src/app/modules/songs/services/text-rendering.service.spec.ts index 6f28483..f765898 100644 --- a/src/app/modules/songs/services/text-rendering.service.spec.ts +++ b/src/app/modules/songs/services/text-rendering.service.spec.ts @@ -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'}, ]); }); diff --git a/src/app/modules/songs/services/text-rendering.service.ts b/src/app/modules/songs/services/text-rendering.service.ts index b1e8708..3f5bb2e 100644 --- a/src/app/modules/songs/services/text-rendering.service.ts +++ b/src/app/modules/songs/services/text-rendering.service.ts @@ -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); } diff --git a/src/app/modules/songs/services/transpose.service.spec.ts b/src/app/modules/songs/services/transpose.service.spec.ts index 8b3ad06..a8642a9 100644 --- a/src/app/modules/songs/services/transpose.service.spec.ts +++ b/src/app/modules/songs/services/transpose.service.spec.ts @@ -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'); + } }); }); diff --git a/src/app/modules/songs/services/transpose.service.ts b/src/app/modules/songs/services/transpose.service.ts index 847e0ea..9ece049 100644 --- a/src/app/modules/songs/services/transpose.service.ts +++ b/src/app/modules/songs/services/transpose.service.ts @@ -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; } diff --git a/src/app/modules/songs/song-list/song-list.component.spec.ts b/src/app/modules/songs/song-list/song-list.component.spec.ts index 1358b77..4fdbab6 100644 --- a/src/app/modules/songs/song-list/song-list.component.spec.ts +++ b/src/app/modules/songs/song-list/song-list.component.spec.ts @@ -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'}]); - })); }); diff --git a/src/app/widget-modules/components/song-text/song-text.component.ts b/src/app/widget-modules/components/song-text/song-text.component.ts index 50a879c..3b6d88b 100644 --- a/src/app/widget-modules/components/song-text/song-text.component.ts +++ b/src/app/widget-modules/components/song-text/song-text.component.ts @@ -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(); @ViewChildren('section') public viewSections: QueryList> | 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) {} @@ -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); } }