transform keys and text service

This commit is contained in:
2020-03-16 21:07:11 +01:00
committed by smuddy
parent 8c000867cb
commit 7d58dd9bdd
18 changed files with 335 additions and 36 deletions

View File

@@ -0,0 +1,95 @@
export const KEYS = [
'C', 'C#', 'Db', 'D', 'D#', 'Eb', 'E', 'F', 'F#', 'Gb', 'G', 'G#', 'Ab', 'A', 'A#', 'B', 'H',
'c', 'c#', 'db', 'd', 'd#', 'eb', 'e', 'f', 'f#', 'gb', 'g', 'g#', 'ab', 'a', 'a#', 'b', 'h'
];
export const KEYS_MAJOR_FLAT = [
'C', 'C#', 'D', 'D#', 'E', 'F', 'F#', 'G', 'G#', 'A', 'A#', 'H',
];
export const KEYS_MAJOR_B = [
'C', 'Db', 'D', 'Eb', 'E', 'F', 'Gb', 'G', 'Ab', 'A', 'B', 'H',
];
export const KEYS_MINOR_FLAT = [
'c', 'c#', 'd', 'd#', 'e', 'f', 'f#', 'g', 'g#', 'a', 'a#', 'h',
];
export const KEYS_MINOR_B = [
'c', 'db', 'd', 'eb', 'e', 'f', 'gb', 'g', 'ab', 'a', 'b', 'h',
];
const scaleAssignment = {
'C': KEYS_MAJOR_FLAT,
'C#': KEYS_MAJOR_FLAT,
'Db': KEYS_MAJOR_B,
'D': KEYS_MAJOR_B,
'D#': KEYS_MAJOR_FLAT,
'Eb': KEYS_MAJOR_B,
'E': KEYS_MAJOR_FLAT,
'F': KEYS_MAJOR_B,
'F#': KEYS_MAJOR_FLAT,
'Gb': KEYS_MAJOR_B,
'G': KEYS_MAJOR_FLAT,
'G#': KEYS_MAJOR_FLAT,
'Ab': KEYS_MAJOR_B,
'A': KEYS_MAJOR_FLAT,
'A#': KEYS_MAJOR_FLAT,
'B': KEYS_MAJOR_B,
'H': KEYS_MAJOR_FLAT,
'c': KEYS_MINOR_FLAT,
'c#': KEYS_MINOR_FLAT,
'db': KEYS_MINOR_B,
'd': KEYS_MINOR_B,
'd#': KEYS_MINOR_FLAT,
'eb': KEYS_MINOR_B,
'e': KEYS_MINOR_FLAT,
'f': KEYS_MINOR_B,
'f#': KEYS_MINOR_FLAT,
'gb': KEYS_MINOR_B,
'g': KEYS_MINOR_FLAT,
'g#': KEYS_MINOR_FLAT,
'ab': KEYS_MINOR_B,
'a': KEYS_MINOR_FLAT,
'a#': KEYS_MINOR_FLAT,
'b': KEYS_MINOR_B,
'h': KEYS_MINOR_FLAT,
};
export const scaleMapping = {
'C': 'C',
'C#': 'C♯',
'Db': 'D♭',
'D': 'D',
'D#': 'D♯',
'Eb': 'E♭',
'E': 'E',
'F': 'F',
'F#': 'F♯',
'Gb': 'D♭',
'G': 'G',
'G#': 'G♯',
'Ab': 'A♭',
'A': 'A',
'A#': 'A♯',
'B': 'B',
'H': 'H',
'c': 'c',
'c#': 'c♯',
'db': 'd♭',
'd': 'd',
'd#': 'd♯',
'eb': 'e♭',
'e': 'e',
'f': 'f',
'f#': 'f♯',
'gb': 'g♭',
'g': 'g',
'g#': 'g♯',
'ab': 'a♭',
'a': 'a',
'a#': 'a♯',
'b': 'b',
'h': 'h',
};
export const getScale = (key: string): string[] => {
const scaleAssignmentElement = scaleAssignment[key];
return scaleAssignmentElement;
};

View File

@@ -11,15 +11,12 @@ declare var importCCLI: any;
})
export class SongService {
public TYPES = ['Praise', 'Worship'];
public static TYPES = ['Praise', 'Worship'];
public static LEGAL_OWNER = ['CCLI', 'other'];
public static LEGAL_TYPE = ['open', 'allowed'];
public LEGAL_OWNER = ['CCLI', 'other'];
public LEGAL_TYPE = ['open', 'allowed'];
public KEYS = [
'C', 'C#', 'Db', 'D', 'D#', 'Eb', 'E', 'F', 'F#', 'Gb', 'G', 'G#', 'Ab', 'A', 'A#', 'B', 'H',
'c', 'c#', 'db', 'd', 'd#', 'eb', 'e', 'f', 'f#', 'gb', 'g', 'g#', 'ab', 'a', 'a#', 'b', 'h'
];
private list: Song[];
constructor(private songDataService: SongDataService) {

View File

@@ -1,12 +1,74 @@
import {TestBed} from '@angular/core/testing';
import {TextRenderingService} from './text-rendering.service';
import {LineType, SectionType, TextRenderingService} from './text-rendering.service';
describe('TextRenderingService', () => {
const testText = `Strophe
C D E F G A H
Text Line 1-1
a d e f g a h c b
Text Line 2-1
Strophe
C D E F G A H
Text Line 1-2
a d e f g a h c b
Text Line 2-2
Refrain
c# cb c7 cmaj7 c/e
and the chorus
Bridge
Cool bridge without any chords
`;
beforeEach(() => TestBed.configureTestingModule({}));
it('should be created', () => {
const service: TextRenderingService = TestBed.get(TextRenderingService);
expect(service).toBeTruthy();
});
it('should parse section types', () => {
const service: TextRenderingService = TestBed.get(TextRenderingService);
const sections = service.parse(testText);
expect(sections[0].type).toBe(SectionType.Verse);
expect(sections[1].type).toBe(SectionType.Verse);
expect(sections[2].type).toBe(SectionType.Chorus);
expect(sections[3].type).toBe(SectionType.Bridge);
});
it('should parse text lines', () => {
const service: TextRenderingService = TestBed.get(TextRenderingService);
const sections = service.parse(testText);
expect(sections[0].lines[1].type).toBe(LineType.text);
expect(sections[0].lines[1].text).toBe('Text Line 1-1');
expect(sections[0].lines[3].type).toBe(LineType.text);
expect(sections[0].lines[3].text).toBe('Text Line 2-1');
expect(sections[1].lines[1].type).toBe(LineType.text);
expect(sections[1].lines[1].text).toBe('Text Line 1-2');
expect(sections[1].lines[3].type).toBe(LineType.text);
expect(sections[1].lines[3].text).toBe('Text Line 2-2');
expect(sections[2].lines[1].type).toBe(LineType.text);
expect(sections[2].lines[1].text).toBe('and the chorus');
expect(sections[3].lines[0].type).toBe(LineType.text);
expect(sections[3].lines[0].text).toBe('Cool bridge without any chords');
});
it('should parse chord lines', () => {
const service: TextRenderingService = TestBed.get(TextRenderingService);
const sections = service.parse(testText);
expect(sections[0].lines[0].type).toBe(LineType.chrod);
expect(sections[0].lines[0].text).toBe('C D E F G A H');
expect(sections[0].lines[2].type).toBe(LineType.chrod);
expect(sections[0].lines[2].text).toBe(' a d e f g a h c b');
expect(sections[1].lines[0].type).toBe(LineType.chrod);
expect(sections[1].lines[0].text).toBe('C D E F G A H');
expect(sections[1].lines[2].type).toBe(LineType.chrod);
expect(sections[1].lines[2].text).toBe(' a d e f g a h c b');
expect(sections[2].lines[0].type).toBe(LineType.chrod);
expect(sections[2].lines[0].text).toBe('c# cb c7 cmaj7 c/e');
});
});

View File

@@ -17,7 +17,6 @@ export interface Line {
text: string;
}
export interface Section {
type: SectionType;
number: number;
@@ -32,5 +31,40 @@ export class TextRenderingService {
constructor() {
}
private regexSection = /(Strophe|Refrain|Bridge)/;
private regexChords = /\b([CDEFGAHBcdefgahb](#|##|b|bb|sus|maj|maj7|min|aug|\d+|\/[CDEFGAHBcdefgahb])?\b)/;
public parse(text: string): Section[] {
const arrayOfLines = text.split(/\r?\n/).filter(_ => _);
const sections = arrayOfLines.reduce((array, line) => {
if (line.match(this.regexSection)) return [...array, {
type: this.getSectionTypeOfLine(line),
number: -1,
lines: []
}];
array[array.length - 1].lines.push(this.getLineOfLineText(line));
return array;
}, [] as Section[]);
return sections;
}
private getLineOfLineText(text: string): Line {
const matches = !!text.match(this.regexChords);
const type = matches ? LineType.chrod : LineType.text;
return {type, text}
}
private getSectionTypeOfLine(line: string): SectionType {
const typeString = line.match(this.regexSection)[1];
switch (typeString) {
case "Strophe":
return SectionType.Verse;
case "Refrain":
return SectionType.Chorus;
case "Bridge":
return SectionType.Bridge;
}
}
}

View File

@@ -16,7 +16,7 @@
<mat-form-field appearance="outline">
<mat-label>Tonart</mat-label>
<mat-select formControlName="key">
<mat-option *ngFor="let key of keys" [value]="key">{{key}}</mat-option>
<mat-option *ngFor="let key of keys" [value]="key">{{key|key}}</mat-option>
</mat-select>
</mat-form-field>
<mat-form-field appearance="outline">

View File

@@ -5,6 +5,7 @@ import {ActivatedRoute, Router} from '@angular/router';
import {SongService} from '../../../services/song.service';
import {EditService} from '../edit.service';
import {first, map, switchMap} from 'rxjs/operators';
import {KEYS} from '../../../services/key.helper';
@Component({
selector: 'app-edit-song',
@@ -14,10 +15,10 @@ import {first, map, switchMap} from 'rxjs/operators';
export class EditSongComponent implements OnInit {
public song: Song;
public form: FormGroup;
public keys = this.songService.KEYS;
public types = this.songService.TYPES;
public legalOwner = this.songService.LEGAL_OWNER;
public legalType = this.songService.LEGAL_TYPE;
public keys = KEYS;
public types = SongService.TYPES;
public legalOwner = SongService.LEGAL_OWNER;
public legalType = SongService.LEGAL_TYPE;
constructor(
private activatedRoute: ActivatedRoute,

View File

@@ -16,6 +16,7 @@ import {MatIconModule} from '@angular/material/icon';
import {FileComponent} from './edit-file/file/file.component';
import {LegalOwnerTranslatorModule} from '../../../../widget-modules/pipes/legal-owner-translator/legal-owner-translator.module';
import {LegalTypeTranslatorModule} from '../../../../widget-modules/pipes/legal-type-translator/legal-type-translator.module';
import {KeyTranslatorModule} from '../../../../widget-modules/pipes/key-translator/key-translator.module';
@NgModule({
@@ -37,6 +38,7 @@ import {LegalTypeTranslatorModule} from '../../../../widget-modules/pipes/legal-
MatIconModule,
LegalOwnerTranslatorModule,
LegalTypeTranslatorModule,
KeyTranslatorModule,
]
})