我的问题是 Web 服务在线程更新其值之前返回识别类中静态变量响应的值(不是最新值(这是我的网络服务:
@GET
@Produces("application/json")
public synchronized String returnTitle() {
String result="init value";
Recognition recognition = new Recognition();
FutureTask<String> future = new FutureTask(recognition);
future.run();
System.out.println("result0 = "+result);
try{
System.out.println("result = "+result);
result = future.get();
System.out.println("result1 = "+result);
}catch (Exception e){e.printStackTrace();}
return "<h1>hello</h1> "+ Recognition.response;
}
此类包含名为 response 的静态变量,该变量由 Web 服务返回。
import java.io.File;
import java.util.List;
import java.util.concurrent.Callable;
import com.bitsinharmony.recognito.MatchResult;
public class Recognition implements Callable<String>{
public static String response =""; // The variable to return by the web service.
@Override
public String call() throws Exception {
response= "";
if (!Ressources.isInitilalized){ // if the initialization of training data is not done yet.
Ressources.init(); // initialize training data
Ressources.isInitilalized= true; //changing the boolean variable to know that inisialization has been done next execution
}
final JavaSoundRecorder recorder = new JavaSoundRecorder(); // Class used to record voice from microphone and save it to .wav file.
Thread stopper = new Thread(new Runnable() { // Thread that sleeps 5000 ms then stops recording.
public void run() {
try {
Thread.sleep(5000);
} catch (InterruptedException ex) {
ex.printStackTrace();
}
recorder.finish(); // after timer finished stop recording
try{
File file20 = new File("C:\RecordAudio.wav");
List<MatchResult<String>> matches = Ressources.recognito.identify(file20);
MatchResult<String> match = matches.get(0);
//file20.delete();
System.out.println(match.getKey() + " " + match.getLikelihoodRatio());
response = match.getKey()+" "+match.getLikelihoodRatio()+"n";// say for which user the sound recorded belongs with witch probability.
}catch (Exception e){}
}
});
stopper.start(); // start the thread
// start recording
recorder.start(); // start recording
return response;
}
}
此类包含静态资源来训练说话人识别模型:
import java.io.File;
import com.bitsinharmony.recognito.Recognito;
public class Ressources {
public static String response = "hello : ";
public final static File file0 = new File("C:\Users\dsghaier\Desktop\Records\silence12.wav");
public final static File file00 = new File("C:\Users\dsghaier\Desktop\Records\noise12.wav");
public final static File file1 = new File("C:\Users\dsghaier\Desktop\Records\dhia12.wav");
public final static File file30 = new File("C:\Users\dsghaier\Desktop\Records\dhia30.wav");
public final static File file2 = new File("C:\Users\dsghaier\Desktop\Records\dhia2.wav");
public final static File file3 = new File("C:\Users\dsghaier\Desktop\Records\obama1.wav");
public final static File file4 = new File("C:\Users\dsghaier\Desktop\Records\obama2.wav");
public final static File file5 = new File("C:\Users\dsghaier\Desktop\Records\obama3.wav");
public final static File file6 = new File("C:\Users\dsghaier\Desktop\Records\reagan1.wav");
public final static File file7 = new File("C:\Users\dsghaier\Desktop\Records\reagan2.wav");
public final static File file8 = new File("C:\Users\dsghaier\Desktop\Records\reagan3.wav");
public final static File file9 = new File("C:\Users\dsghaier\Desktop\Records\georgebush1.wav");
public final static File file10 = new File("C:\Users\dsghaier\Desktop\Records\georgebush2.wav");
public final static File file11 = new File("C:\Users\dsghaier\Desktop\Records\georgebush3.wav");
public final static File file12 = new File("C:\Users\dsghaier\Desktop\Records\georgebush4.wav");
public final static File file13 = new File("C:\Users\dsghaier\Desktop\Records\carter1.wav");
public final static File file14 = new File("C:\Users\dsghaier\Desktop\Records\carter2.wav");
public final static File file15 = new File("C:\Users\dsghaier\Desktop\Records\clinton1.wav");
public final static File file16 = new File("C:\Users\dsghaier\Desktop\Records\clinton2.wav");
public final static File file17 = new File("C:\Users\dsghaier\Desktop\Records\huyan1.wav");
public final static File file18 = new File("C:\Users\dsghaier\Desktop\Records\huyan2.wav");
public final static File file19 = new File("C:\Users\dsghaier\Desktop\Records\huyan3.wav");
public static Recognito<String> recognito = new Recognito<String>(16000.0f);
public static boolean isInitilalized = false;
public static void init(){
try{
recognito.createVoicePrint("Dhia", file1);
//recognito.mergeVoiceSample("Dhia", file2);
//recognito.mergeVoiceSample("Dhia", file30);
recognito.createVoicePrint("Obama", file3);
//recognito.mergeVoiceSample("Obama", file4);
//recognito.mergeVoiceSample("Obama", file5);
recognito.createVoicePrint("Reagan", file6);
//recognito.mergeVoiceSample("Reagan", file7);
//recognito.mergeVoiceSample("Reagan", file8);
recognito.createVoicePrint("George Bush", file9);
//recognito.mergeVoiceSample("George Bush", file10);
//recognito.mergeVoiceSample("George Bush", file11);
//recognito.mergeVoiceSample("George Bush", file12);
recognito.createVoicePrint("Carter", file13);
//recognito.mergeVoiceSample("Carter", file14);
//recognito.createVoicePrint("Clinton", file15);
//recognito.mergeVoiceSample("Clinton", file16);
//recognito.createVoicePrint("Huyan", file17);
//recognito.mergeVoiceSample("Huyan", file18);
//recognito.mergeVoiceSample("Huyan", file19);
//recognito.createVoicePrint("Silence", file0);
//recognito.createVoicePrint("Noise", file00);
// ********************************
}catch (Exception e){e.printStackTrace();}
isInitilalized = true;
}
}
最后这个类用于录制声音并将其保存到.wav文件中:
import javax.sound.sampled.*;
import java.io.*;
/**
* A sample program is to demonstrate how to record sound in Java
* author: www.codejava.net
*/
public class JavaSoundRecorder {
// record duration, in milliseconds
static final long RECORD_TIME = 500;
// path of the wav file
File wavFile = new File("C:/RecordAudio.wav");
// format of audio file
AudioFileFormat.Type fileType = AudioFileFormat.Type.WAVE;
// the line from which audio data is captured
TargetDataLine line;
/**
* Defines an audio format
*/
AudioFormat getAudioFormat() {
float sampleRate = 16000;
int sampleSizeInBits = 8;
int channels = 1;
boolean signed = true;
boolean bigEndian = true;
AudioFormat format = new AudioFormat(sampleRate, sampleSizeInBits,
channels, signed, bigEndian);
return format;
}
/**
* Captures the sound and record into a WAV file
*/
public void start() {
try {
AudioFormat format = getAudioFormat();
DataLine.Info info = new DataLine.Info(TargetDataLine.class, format);
// checks if system supports the data line
if (!AudioSystem.isLineSupported(info)) {
System.out.println("Line not supported");
System.exit(0);
}
line = (TargetDataLine) AudioSystem.getLine(info);
line.open(format);
line.start(); // start capturing
// System.out.println("Start capturing...");
AudioInputStream ais = new AudioInputStream(line);
// System.out.println("Start recording...");
// start recording
AudioSystem.write(ais, fileType, wavFile);
} catch (LineUnavailableException ex) {
ex.printStackTrace();
} catch (IOException ioe) {
ioe.printStackTrace();
}
}
/**
* Closes the target data line to finish capturing and recording
*/
public void finish() {
line.stop();
line.close();
// System.out.println("Finished");
}
}
也许你应该初始化你的静态变量。初始化和声明变量是不一样的:
- 正在初始化 :
int a = 0;
- 声明 :
int a;
如果这不是您要找的,也许您可能会更具体一点,并向我们显示错误消息