如何保存使用 ttslib 的处理草图生成的音频?
How do I save audio generated by a Processing sketch that uses ttslib?
我正在处理使用 rita 和 ttslib 库生成语音的 Processing sketch(rita 用于生成马尔可夫文本链,而 ttslib 正在将该文本转换为语音)。
我似乎不知道该怎么做,就是将该演讲作为音频文件保存到我的电脑上。这是我当前的代码:
import ddf.minim.*;
import rita.*;
import guru.ttslib.*;
Minim minim;
AudioOutput out;
AudioRecorder recorder;
TTS voice1, voice2, voice3;
RiMarkov rm;
String[] sourceTextArray, script;
String title, sourceText;
int targetScriptLength;
void setup() {
noLoop();
minim = new Minim(this);
out = minim.getLineOut();
recorder = minim.createRecorder(out, "recording.wav");
System.setProperty("mbrola.base","C:\Users\kandr\Desktop\Dropbox\Coding & Web Design\Processing 3\libraries\mbrola\");
}
void draw() {
generateScript("https://en.wikipedia.org/wiki/Special:Random");
prepareVoices();
recorder.beginRecord();
speakScript(script);
recorder.endRecord();
recorder.save();
}
void prepareVoices() {
voice1 = new TTS("mbrola_us1");
voice1.setPitch(180.0);
voice1.setPitchRange(25.0);
voice2 = new TTS("mbrola_us3");
voice2.setPitch(120.0);
voice2.setPitchRange(15.0);
voice3 = new TTS("mbrola_us3");
voice3.setPitch(0.0);
}
void generateScript(String sourceURL) {
sourceTextArray = loadStrings(sourceURL);
sourceText = join(sourceTextArray, " ");
// find random article's title
title = sourceText.substring(sourceText.indexOf("<h1 id=\"firstHeading\" class=\"firstHeading\" lang=\"en\">") + 53, sourceText.indexOf("</h1>"));
title = title.replaceAll("<.*?>", "");
// load the source text into our Markov generator
rm = new RiMarkov(3);
rm.loadText(sourceText);
// set the target script size and instantiate an array to hold the lines
targetScriptLength = 20;
script = new String[targetScriptLength];
// generate lines to populate the script
for (int l = 0; l < targetScriptLength; l++) {
script[l] = rm.generateSentence();
}
}
void speakScript(String[] scriptArr) {
for (int s = 0; s < scriptArr.length; s+=2) {
voice1.speak(scriptArr[s]);
voice2.speak(scriptArr[s+1]);
}
}
如您所见,我尝试使用 Minim 库的 AudioRecorder class 来录制我的草图输出的音频。但是,没有实际的音频被录制——我只是得到一个空的 wav 文件。我还尝试将 AudioRecorder 对象的可录制源设置为 AudioInput,但这只是录制了笔记本电脑麦克风的声音。
我也查看了 Processing 附带的声音库,但没有找到合适的 class 来录制音频。
任何人都可以指出正确的方向来弄清楚如何让我的草图记录它通过 ttslib 生成的音频吗?
首先,即使您编辑了问题,您发布的代码仍然需要额外的文件,因此无法进行测试。
无论如何,问题似乎出在 Minim 的 API,因此音频路由需要从草图外部完成。因为你在windows上工作,就像把录音设备换成"Stereo Mix"一样简单。这会将系统的音频输出重新路由到输入中,因此可以通过最小值进行记录。
下面是我使用ttslib example and the minim recorder example改编的小草图,它记录了ttslib库产生的语音。如果音频从系统设置中重新路由。
import ddf.minim.*;
import ddf.minim.ugens.*;
import guru.ttslib.*;
Minim minim;
TTS tts;
AudioInput in;
AudioRecorder recorder;
boolean recorded;
AudioOutput out;
FilePlayer player;
void setup()
{
size(512, 200, P3D);
tts = new TTS();
minim = new Minim(this);
in = minim.getLineIn();
recorder = minim.createRecorder(in, "test.wav", true);
out = minim.getLineOut( Minim.STEREO );
textFont(createFont("Arial", 12));
}
void draw()
{
background(0);
stroke(255);
for(int i = 0; i < in.left.size()-1; i++)
{
line(i, 50 + in.left.get(i)*50, i+1, 50 + in.left.get(i+1)*50);
line(i, 150 + in.right.get(i)*50, i+1, 150 + in.right.get(i+1)*50);
}
if ( recorder.isRecording() )
{
text("Now recording, press the r key to stop recording.", 5, 15);
}
else if ( !recorded )
{
text("Press the r key to start recording.", 5, 15);
}
else
{
text("Press the s key to save the recording to disk and play it back in the sketch.", 5, 15);
}
}
void keyReleased()
{
if ( !recorded && key == 'r' )
{
if ( recorder.isRecording() )
{
recorder.endRecord();
recorded = true;
}
else
{
recorder.beginRecord();
}
}
if ( recorded && key == 's' )
{
if ( player != null )
{
player.unpatch( out );
player.close();
}
player = new FilePlayer( recorder.save() );
player.patch( out );
player.play();
}
}
void mousePressed() {
tts.speak("Hi! This voice is being recorded");
}
我正在处理使用 rita 和 ttslib 库生成语音的 Processing sketch(rita 用于生成马尔可夫文本链,而 ttslib 正在将该文本转换为语音)。
我似乎不知道该怎么做,就是将该演讲作为音频文件保存到我的电脑上。这是我当前的代码:
import ddf.minim.*;
import rita.*;
import guru.ttslib.*;
Minim minim;
AudioOutput out;
AudioRecorder recorder;
TTS voice1, voice2, voice3;
RiMarkov rm;
String[] sourceTextArray, script;
String title, sourceText;
int targetScriptLength;
void setup() {
noLoop();
minim = new Minim(this);
out = minim.getLineOut();
recorder = minim.createRecorder(out, "recording.wav");
System.setProperty("mbrola.base","C:\Users\kandr\Desktop\Dropbox\Coding & Web Design\Processing 3\libraries\mbrola\");
}
void draw() {
generateScript("https://en.wikipedia.org/wiki/Special:Random");
prepareVoices();
recorder.beginRecord();
speakScript(script);
recorder.endRecord();
recorder.save();
}
void prepareVoices() {
voice1 = new TTS("mbrola_us1");
voice1.setPitch(180.0);
voice1.setPitchRange(25.0);
voice2 = new TTS("mbrola_us3");
voice2.setPitch(120.0);
voice2.setPitchRange(15.0);
voice3 = new TTS("mbrola_us3");
voice3.setPitch(0.0);
}
void generateScript(String sourceURL) {
sourceTextArray = loadStrings(sourceURL);
sourceText = join(sourceTextArray, " ");
// find random article's title
title = sourceText.substring(sourceText.indexOf("<h1 id=\"firstHeading\" class=\"firstHeading\" lang=\"en\">") + 53, sourceText.indexOf("</h1>"));
title = title.replaceAll("<.*?>", "");
// load the source text into our Markov generator
rm = new RiMarkov(3);
rm.loadText(sourceText);
// set the target script size and instantiate an array to hold the lines
targetScriptLength = 20;
script = new String[targetScriptLength];
// generate lines to populate the script
for (int l = 0; l < targetScriptLength; l++) {
script[l] = rm.generateSentence();
}
}
void speakScript(String[] scriptArr) {
for (int s = 0; s < scriptArr.length; s+=2) {
voice1.speak(scriptArr[s]);
voice2.speak(scriptArr[s+1]);
}
}
如您所见,我尝试使用 Minim 库的 AudioRecorder class 来录制我的草图输出的音频。但是,没有实际的音频被录制——我只是得到一个空的 wav 文件。我还尝试将 AudioRecorder 对象的可录制源设置为 AudioInput,但这只是录制了笔记本电脑麦克风的声音。
我也查看了 Processing 附带的声音库,但没有找到合适的 class 来录制音频。
任何人都可以指出正确的方向来弄清楚如何让我的草图记录它通过 ttslib 生成的音频吗?
首先,即使您编辑了问题,您发布的代码仍然需要额外的文件,因此无法进行测试。
无论如何,问题似乎出在 Minim 的 API,因此音频路由需要从草图外部完成。因为你在windows上工作,就像把录音设备换成"Stereo Mix"一样简单。这会将系统的音频输出重新路由到输入中,因此可以通过最小值进行记录。
下面是我使用ttslib example and the minim recorder example改编的小草图,它记录了ttslib库产生的语音。如果音频从系统设置中重新路由。
import ddf.minim.*;
import ddf.minim.ugens.*;
import guru.ttslib.*;
Minim minim;
TTS tts;
AudioInput in;
AudioRecorder recorder;
boolean recorded;
AudioOutput out;
FilePlayer player;
void setup()
{
size(512, 200, P3D);
tts = new TTS();
minim = new Minim(this);
in = minim.getLineIn();
recorder = minim.createRecorder(in, "test.wav", true);
out = minim.getLineOut( Minim.STEREO );
textFont(createFont("Arial", 12));
}
void draw()
{
background(0);
stroke(255);
for(int i = 0; i < in.left.size()-1; i++)
{
line(i, 50 + in.left.get(i)*50, i+1, 50 + in.left.get(i+1)*50);
line(i, 150 + in.right.get(i)*50, i+1, 150 + in.right.get(i+1)*50);
}
if ( recorder.isRecording() )
{
text("Now recording, press the r key to stop recording.", 5, 15);
}
else if ( !recorded )
{
text("Press the r key to start recording.", 5, 15);
}
else
{
text("Press the s key to save the recording to disk and play it back in the sketch.", 5, 15);
}
}
void keyReleased()
{
if ( !recorded && key == 'r' )
{
if ( recorder.isRecording() )
{
recorder.endRecord();
recorded = true;
}
else
{
recorder.beginRecord();
}
}
if ( recorded && key == 's' )
{
if ( player != null )
{
player.unpatch( out );
player.close();
}
player = new FilePlayer( recorder.save() );
player.patch( out );
player.play();
}
}
void mousePressed() {
tts.speak("Hi! This voice is being recorded");
}