forked from discordier/sam
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Rework ABI to allow export of wave buffers
As requested in discordier#1, the wave buffer generating routines should be exported. However, the suggested way of adding multiple exports to the module then broke the common-js builds as there may only be one default export. Therefore we now have multiple methods being exported by the main class.
- Loading branch information
1 parent
561ccb8
commit 80b1808
Showing
7 changed files
with
196 additions
and
70 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,40 +1,84 @@ | ||
import {PlayBuffer, RenderBuffer} from './util/player.es6' | ||
import TextToPhonemes from './reciter/reciter.es6'; | ||
import {SamSpeak} from './sam/sam.es6'; | ||
import {SamProcess, SamBuffer} from './sam/sam.es6'; | ||
|
||
const convert = TextToPhonemes; | ||
const buf8 = SamProcess; | ||
const buf32 = SamBuffer; | ||
|
||
/** | ||
* @param {object} [options] | ||
* @param {Boolean} [options.phonetic] Default false. | ||
* @param {Boolean} [options.singmode] Default false. | ||
* @param {Boolean} [options.debug] Default false. | ||
* @param {Number} [options.pitch] Default 64. | ||
* @param {Number} [options.speed] Default 72. | ||
* @param {Number} [options.mouth] Default 128. | ||
* @param {Number} [options.throat] Default 128. | ||
* | ||
* @constructor | ||
*/ | ||
function SamJs (options) { | ||
const opts = options || {}; | ||
|
||
const convert = this.convert = (text) => { | ||
let input = TextToPhonemes(text); | ||
if (!input) { | ||
if (process.env.DEBUG_SAM === true) { | ||
throw new Error(`phonetic input: "${text}" could not be converted`); | ||
} | ||
throw new Error(); | ||
const ensurePhonetic = (text, phonetic) => { | ||
if (!(phonetic || opts.phonetic)) { | ||
return convert(text); | ||
} | ||
return text.toUpperCase(); | ||
} | ||
|
||
if (process.env.DEBUG_SAM === true) { | ||
console.log('phonetic data: "%s"', input); | ||
} | ||
/** | ||
* Render the passed text as 8bit wave buffer array. | ||
* | ||
* @param {string} text The text to render or phoneme string. | ||
* @param {boolean} [phonetic] Flag if the input text is already phonetic data. | ||
* | ||
* @return {Uint8Array|Boolean} | ||
*/ | ||
this.buf8 = (text, phonetic) => { | ||
return buf8(ensurePhonetic(text, phonetic), opts); | ||
} | ||
|
||
return input; | ||
}; | ||
/** | ||
* Render the passed text as 32bit wave buffer array. | ||
* | ||
* @param {string} text The text to render or phoneme string. | ||
* @param {boolean} [phonetic] Flag if the input text is already phonetic data. | ||
* | ||
* @return {Float32Array|Boolean} | ||
*/ | ||
this.buf32 = (text, phonetic) => { | ||
return buf32(ensurePhonetic(text, phonetic), opts); | ||
} | ||
|
||
/** | ||
* Render the passed text as wave buffer and play it over the speakers. | ||
* | ||
* @param {string} text The text to render or phoneme string. | ||
* @param {boolean} [phonetic] Flag if the input text is already phonetic data. | ||
* | ||
* @return {Promise} | ||
*/ | ||
this.speak = (text, phonetic) => { | ||
if (process.env.DEBUG_SAM === true) { | ||
console.log('text input: ', text); | ||
} | ||
|
||
let input; | ||
return PlayBuffer(this.buf32(text, phonetic)); | ||
} | ||
|
||
if (!(phonetic || opts.phonetic)) { | ||
input = convert(text); | ||
} else { | ||
input = text.toUpperCase(); | ||
} | ||
|
||
return SamSpeak(input, opts); | ||
}; | ||
/** | ||
* Render the passed text as wave buffer and download it via URL API. | ||
* | ||
* @param {string} text The text to render or phoneme string. | ||
* @param {boolean} [phonetic] Flag if the input text is already phonetic data. | ||
* | ||
* @return void | ||
*/ | ||
this.download = (text, phonetic) => { | ||
RenderBuffer(this.buf8(text, phonetic)); | ||
} | ||
} | ||
|
||
SamJs.buf8 = buf8; | ||
SamJs.buf32 = buf32; | ||
SamJs.convert = convert; | ||
|
||
export default SamJs; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,77 @@ | ||
import { assert } from 'chai' | ||
|
||
import SamJs from '../src/index.es6'; | ||
|
||
describe('index.es6', () => { | ||
describe('SamJs', () => { | ||
it('should have method buf8', () => { | ||
const sam = new SamJs({}); | ||
assert.isDefined(sam.buf8); | ||
}) | ||
it('should have method buf32', () => { | ||
const sam = new SamJs({}); | ||
assert.isDefined(sam.buf32); | ||
}) | ||
it('should have method speak', () => { | ||
const sam = new SamJs({}); | ||
assert.isDefined(sam.speak); | ||
}) | ||
it('should speak', () => { | ||
let bufferLength, setBuffer | ||
const source = { | ||
buffer: { | ||
set (buffer) { | ||
assert.strictEqual(buffer, soundBuffer) | ||
} | ||
}, | ||
connect (destination) { | ||
assert.strictEqual(destination, context.destination) | ||
}, | ||
start (when) { | ||
assert.strictEqual(when, 0) | ||
assert.notEqual(setBuffer, undefined) | ||
assert.strictEqual(setBuffer.length, bufferLength) | ||
assert.notEqual(this.onended, undefined) | ||
this.onended() | ||
} | ||
}; | ||
const context = { | ||
createBufferSource () { | ||
return source | ||
}, | ||
createBuffer(numberOfChannels, length, sampleRate) { | ||
bufferLength = length | ||
assert.strictEqual(numberOfChannels, 1) | ||
assert.notEqual(length, 0) | ||
assert.strictEqual(sampleRate, 22050) | ||
return soundBuffer | ||
}, | ||
destination: {} | ||
}; | ||
const soundBuffer = { | ||
getChannelData (channel) { | ||
assert.strictEqual(channel, 0) | ||
return setBuffer = [] | ||
} | ||
}; | ||
global.AudioContext = function () { | ||
return context | ||
} | ||
|
||
const sam = new SamJs({}); | ||
return sam.speak('/HEHLOW').then( | ||
() => { | ||
delete global.AudioContext | ||
}, | ||
(e) => { | ||
delete global.AudioContext | ||
console.log(e) | ||
assert.fail('Failed to play.'); | ||
}); | ||
}) | ||
}); | ||
it('should have method download', () => { | ||
const sam = new SamJs({}); | ||
assert.isDefined(sam.download); | ||
}) | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters