Skip to content

Commit

Permalink
Merge pull request discordier#32 from tyomitch
Browse files Browse the repository at this point in the history
Major refactoring to remove BREAK & END markers.
  • Loading branch information
discordier committed Jun 1, 2022
2 parents 744f808 + 21ee480 commit 747e0b7
Show file tree
Hide file tree
Showing 19 changed files with 391 additions and 417 deletions.
2 changes: 0 additions & 2 deletions src/common/constants.es6

This file was deleted.

12 changes: 5 additions & 7 deletions src/parser/adjust-lengths.es6
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,6 @@ import {
phonemeFlags,
} from './tables.es6';

import {END} from '../common/constants.es6'

import {
FLAG_PUNCT,
FLAG_NASAL,
Expand Down Expand Up @@ -52,7 +50,7 @@ export default (getPhoneme, setLength, getLength) => {
// increased by (length * 1.5) + 1

// loop index
for (let position = 0;getPhoneme(position) !== END;position++) {
for (let position = 0;getPhoneme(position) !== null;position++) {
// not punctuation?
if(!phonemeHasFlag(getPhoneme(position), FLAG_PUNCT)) {
continue;
Expand Down Expand Up @@ -89,7 +87,7 @@ export default (getPhoneme, setLength, getLength) => {
let loopIndex = -1;
let phoneme;

while((phoneme = getPhoneme(++loopIndex)) !== END) {
while((phoneme = getPhoneme(++loopIndex)) !== null) {
let position = loopIndex;
// vowel?
if (phonemeHasFlag(phoneme, FLAG_VOWEL)) {
Expand Down Expand Up @@ -117,7 +115,7 @@ export default (getPhoneme, setLength, getLength) => {
}
// Got here if not <VOWEL>
// FIXME: the case when phoneme === END is taken over by !phonemeHasFlag(phoneme, FLAG_CONSONANT)
let flags = (phoneme === END) ? (FLAG_CONSONANT | FLAG_UNVOICED_STOPCONS) : phonemeFlags[phoneme];
let flags = (phoneme === null) ? (FLAG_CONSONANT | FLAG_UNVOICED_STOPCONS) : phonemeFlags[phoneme];
// Unvoiced
if (!matchesBitmask(flags, FLAG_VOICED)) {
// *, .*, ?*, ,*, -*, DX, S*, SH, F*, TH, /H, /X, CH, P*, T*, K*, KX
Expand Down Expand Up @@ -159,7 +157,7 @@ export default (getPhoneme, setLength, getLength) => {
// M*, N*, NX,
phoneme = getPhoneme(++position);
// is next phoneme a stop consonant?
if (phoneme !== END && phonemeHasFlag(phoneme, FLAG_STOPCONS)) {
if (phoneme !== null && phonemeHasFlag(phoneme, FLAG_STOPCONS)) {
// B*, D*, G*, GX, P*, T*, K*, KX
if (process.env.DEBUG_SAM === true) {
console.log(`${position} RULE: <NASAL> <STOP CONSONANT> - set nasal = 5, consonant = 6`);
Expand All @@ -182,7 +180,7 @@ export default (getPhoneme, setLength, getLength) => {

while ((phoneme = getPhoneme(++position)) === 0) { /* move past silence */ }
// if another stop consonant, process.
if (phoneme !== END && phonemeHasFlag(phoneme, FLAG_STOPCONS)) {
if (phoneme !== null && phonemeHasFlag(phoneme, FLAG_STOPCONS)) {
// RULE: <STOP CONSONANT> {optional silence} <STOP CONSONANT>
if (process.env.DEBUG_SAM === true) {
console.log(
Expand Down
38 changes: 1 addition & 37 deletions src/parser/c-conv/parser.es6
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ import {
FLAG_UNVOICED_STOPCONS,
} from '../constants.es6'

import {BREAK, END} from '../../common/constants.es6'
const END = 255;

import {text2Uint8Array} from '../../../src/util/util.es6';

Expand Down Expand Up @@ -719,39 +719,6 @@ function Code41240({phonemeindex, phonemeLength, stress}) {
* @return undefined
*/
function InsertBreath({phonemeindex, phonemeLength, stress}) {
let pausePos = 255;
let len = 0; // mem55
let phoneme; //variable Y
let pos = 0; // mem66

while(((phoneme = phonemeindex[pos]) !== END) && (pos<phonemeindex.length)) {
//pos48440:
phoneme = phonemeindex[pos];
len += phonemeLength[pos];
if (len < 232)
{
if (phoneme !== 254) // ML : Prevents an index out of bounds problem
{
// console.log("%s: flags2[%s] == %s&1 == %s\n", pos, phoneme, flags[phoneme], (flags[phoneme]&FLAG_DIPTHONG));
if((flags[phoneme]&FLAG_PUNCT) !== 0)
{
len = 0;
Insert({phonemeindex, phonemeLength, stress}, pos + 1, BREAK, 0, 0);
pos += 2;
continue;
}
}
if (phoneme === 0) { pausePos = pos; }
pos++;
continue;
}
phonemeindex[pausePos] = 31; // 'Q*' glottal stop
phonemeLength[pausePos] = 4;
stress[pausePos] = 0;
len = 0;
Insert({phonemeindex, phonemeLength, stress}, pausePos + 1, BREAK, 0, 0);
pos = pausePos + 2;
}
}

/**
Expand Down Expand Up @@ -837,9 +804,6 @@ function PrintPhonemes ({phonemeindex, phonemeLength, stress}) {
if (phonemeindex[i] < 81) {
return String.fromCharCode(signInputTable1[phonemeindex[i]], signInputTable2[phonemeindex[i]]);
}
if (phoneme === BREAK) {
return ' ';
}
return '??'
};
console.log(
Expand Down
5 changes: 2 additions & 3 deletions src/parser/copy-stress.es6
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import {END} from '../common/constants.es6'
import {FLAG_VOWEL, FLAG_CONSONANT} from './constants.es6'

import { phonemeHasFlag } from './util.es6';
Expand Down Expand Up @@ -26,12 +25,12 @@ export default (getPhoneme, getStress, setStress) => {
// loop through all the phonemes to be output
let position = 0;
let phoneme;
while((phoneme = getPhoneme(position)) !== END) {
while((phoneme = getPhoneme(position)) !== null) {
// if CONSONANT_FLAG set, skip - only vowels get stress
if (phonemeHasFlag(phoneme, FLAG_CONSONANT)) {
phoneme = getPhoneme(position + 1);
// if the following phoneme is the end, or a vowel, skip
if ((phoneme !== END) && phonemeHasFlag(phoneme, FLAG_VOWEL)) {
if ((phoneme !== null) && phonemeHasFlag(phoneme, FLAG_VOWEL)) {
// get the stress value at the next position
let stress = getStress(position + 1);
if ((stress !== 0) && (stress < 0x80)) {
Expand Down
42 changes: 0 additions & 42 deletions src/parser/insert-breath.es6

This file was deleted.

15 changes: 7 additions & 8 deletions src/parser/parse2.es6
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import {END} from '../common/constants.es6'
import { PhonemeNameTable } from './tables.es6';
import { phonemeHasFlag } from './util.es6';
import {
Expand Down Expand Up @@ -91,7 +90,7 @@ export default (insertPhoneme, setPhoneme, getPhoneme, getStress) => {
let pos = -1;
let phoneme;

while((phoneme = getPhoneme(++pos)) !== END) {
while((phoneme = getPhoneme(++pos)) !== null) {
// Is phoneme pause?
if (phoneme === 0) {
continue;
Expand Down Expand Up @@ -137,9 +136,9 @@ export default (insertPhoneme, setPhoneme, getPhoneme, getStress) => {
// RULE:
// <STRESSED VOWEL> <SILENCE> <STRESSED VOWEL> -> <STRESSED VOWEL> <SILENCE> Q <VOWEL>
// EXAMPLE: AWAY EIGHT
if (!getPhoneme(pos+1)) { // If following phoneme is a pause, get next
if (getPhoneme(pos+1) === 0) { // If following phoneme is a pause, get next
phoneme = getPhoneme(pos+2);
if (phoneme !== END && phonemeHasFlag(phoneme, FLAG_VOWEL) && getStress(pos+2)) {
if (phoneme !== null && phonemeHasFlag(phoneme, FLAG_VOWEL) && getStress(pos+2)) {
if (process.env.DEBUG_SAM === true) {
console.log(`${pos+2} RULE: Insert glottal stop between two stressed vowels with space between them`);
}
Expand All @@ -149,7 +148,7 @@ export default (insertPhoneme, setPhoneme, getPhoneme, getStress) => {
continue;
}

let priorPhoneme = (pos === 0) ? END : getPhoneme(pos - 1);
let priorPhoneme = (pos === 0) ? null : getPhoneme(pos - 1);

if (phoneme === pR) {
// RULES FOR PHONEMES BEFORE R
Expand Down Expand Up @@ -200,7 +199,7 @@ export default (insertPhoneme, setPhoneme, getPhoneme, getStress) => {
// Example: GO
let phoneme = getPhoneme(pos + 1);
// If diphthong ending with YX, move continue processing next phoneme
if (!phonemeHasFlag(phoneme, FLAG_DIP_YX) && (phoneme !== END)) {
if (!phonemeHasFlag(phoneme, FLAG_DIP_YX) && (phoneme !== null)) {
// replace G with GX and continue processing next phoneme
if (process.env.DEBUG_SAM === true) {
console.log(
Expand All @@ -218,7 +217,7 @@ export default (insertPhoneme, setPhoneme, getPhoneme, getStress) => {
// Example: COW
let Y = getPhoneme(pos + 1);
// If at end, replace current phoneme with KX
if (!phonemeHasFlag(Y, FLAG_DIP_YX) || Y === END) {
if (!phonemeHasFlag(Y, FLAG_DIP_YX) || Y === null) {
// VOWELS AND DIPHTHONGS ENDING WITH IY SOUND flag set?
if (process.env.DEBUG_SAM === true) {
console.log(`${pos} K <VOWEL OR DIPTHONG NOT ENDING WITH IY> -> KX <VOWEL OR DIPTHONG NOT ENDING WITH IY>`);
Expand Down Expand Up @@ -255,7 +254,7 @@ export default (insertPhoneme, setPhoneme, getPhoneme, getStress) => {
// Example: PARTY, TARDY
if ((pos > 0) && phonemeHasFlag(getPhoneme(pos-1), FLAG_VOWEL)) {
phoneme = getPhoneme(pos + 1);
if (!phoneme) {
if (phoneme === 0) {
phoneme = getPhoneme(pos + 2);
}
if (phonemeHasFlag(phoneme, FLAG_VOWEL) && !getStress(pos+1)) {
Expand Down
21 changes: 3 additions & 18 deletions src/parser/parser.es6
Original file line number Diff line number Diff line change
@@ -1,11 +1,9 @@
import {BREAK, END} from '../common/constants.es6'
import {PhonemeNameTable} from './tables.es6';
import Parser1 from './parse1.es6';
import Parser2 from './parse2.es6';
import AdjustLengths from './adjust-lengths.es6';
import CopyStress from './copy-stress.es6';
import SetPhonemeLength from './set-phoneme-length.es6';
import InsertBreath from './insert-breath.es6';
import ProlongPlosiveStopConsonantsCode41240 from './prolong-plosive-stop-consonants.es6';

/**
Expand All @@ -27,7 +25,7 @@ export default (input) => {
throw new Error('Out of bounds: ' + pos)
}
}
return (pos === phonemeindex.length - 1) ? END : phonemeindex[pos]
return (pos === phonemeindex.length) ? null : phonemeindex[pos]
};
const setPhoneme = (pos, value) => {
if (process.env.DEBUG_SAM === true) {
Expand Down Expand Up @@ -103,7 +101,6 @@ export default (input) => {
stress[pos - 1] = value; /* Set stress for prior phoneme */
}
);
phonemeindex[pos] = END;

if (process.env.DEBUG_SAM === true) {
PrintPhonemes(phonemeindex, phonemeLength, stress);
Expand All @@ -114,21 +111,12 @@ export default (input) => {
AdjustLengths(getPhoneme, setLength, getLength);
ProlongPlosiveStopConsonantsCode41240(getPhoneme, insertPhoneme, getStress);

for (let i = 0;i<phonemeindex.length;i++) {
if (phonemeindex[i] > 80) {
phonemeindex[i] = END;
// FIXME: When will this ever be anything else than END?
break; // error: delete all behind it
}
}

InsertBreath(getPhoneme, setPhoneme, insertPhoneme, getStress, getLength, setLength);

if (process.env.DEBUG_SAM === true) {
PrintPhonemes(phonemeindex, phonemeLength, stress);
}

return phonemeindex.map((v, i) => [v, phonemeLength[i] | 0, stress[i] | 0]);
return phonemeindex.map((v, i) => v ? [v, phonemeLength[i] | 0, stress[i] | 0] : null)
.filter(v => v);
}

/**
Expand All @@ -155,9 +143,6 @@ const PrintPhonemes = (phonemeindex, phonemeLength, stress) => {
if (phonemeindex[i] < 81) {
return PhonemeNameTable[phonemeindex[i]];
}
if (phoneme === BREAK) {
return ' ';
}
return '??'
};
console.log(
Expand Down
5 changes: 2 additions & 3 deletions src/parser/prolong-plosive-stop-consonants.es6
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import {END} from '../common/constants.es6'
import {combinedPhonemeLengthTable} from './tables.es6';
import {FLAG_0008, FLAG_STOPCONS, FLAG_UNVOICED_STOPCONS} from './constants.es6'
import { phonemeHasFlag } from './util.es6';
Expand All @@ -16,7 +15,7 @@ import { phonemeHasFlag } from './util.es6';
export default (getPhoneme, insertPhoneme, getStress) => {
let pos=-1;
let index;
while ((index = getPhoneme(++pos)) !== END) {
while ((index = getPhoneme(++pos)) !== null) {
// Not a stop consonant, move to next one.
if (!phonemeHasFlag(index, FLAG_STOPCONS)) {
continue;
Expand All @@ -27,7 +26,7 @@ export default (getPhoneme, insertPhoneme, getStress) => {
let X = pos;
do { nextNonEmpty = getPhoneme(++X); } while (nextNonEmpty === 0);
// If not END and either flag 0x0008 or '/H' or '/X'
if ((nextNonEmpty !== END)
if ((nextNonEmpty !== null)
&& (
phonemeHasFlag(nextNonEmpty, FLAG_0008)
|| (nextNonEmpty === 36)
Expand Down
3 changes: 1 addition & 2 deletions src/parser/set-phoneme-length.es6
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import {combinedPhonemeLengthTable} from './tables.es6';
import {END} from '../common/constants.es6'

/**
* change phoneme length dependent on stress
Expand All @@ -13,7 +12,7 @@ import {END} from '../common/constants.es6'
export default (getPhoneme, getStress, setLength) => {
let position = 0;
let phoneme;
while((phoneme = getPhoneme(position)) !== END) {
while((phoneme = getPhoneme(position)) !== null) {
let stress = getStress(position);
if ((stress === 0) || (stress > 0x7F)) {
setLength(position, combinedPhonemeLengthTable[phoneme] & 0xFF);
Expand Down
2 changes: 1 addition & 1 deletion src/renderer/create-transitions.es6
Original file line number Diff line number Diff line change
Expand Up @@ -153,5 +153,5 @@ export default (pitches, frequency, amplitude, tuples) => {
}

// add the length of last phoneme
return (boundary + tuples[tuples.length - 1][1]) & 0xFF;
return (boundary + tuples[tuples.length - 1][1]);
}
Loading

0 comments on commit 747e0b7

Please sign in to comment.