Java 将 wav 文件中的语音转为文本

发布于 2024-10-27 03:51:48 字数 5795 浏览 1 评论 0原文

是否可以从java识别wav文件是否可以使用cloudgarden java语音api 他们是来自cloudgarden示例的示例代码,我们自己的wav文件需要进行任何更改 示例代码如下

import javax.speech.*;
import javax.speech.recognition.*;
import java.io.*;
import com.cloudgarden.audio.*;
import com.cloudgarden.speech.*;
import com.cloudgarden.speech.userinterface.SpeechEngineChooser;

/**
 * Tests running a dictation grammar against input from a wave file.
 */

public class DictationFromFile {
    static Recognizer rec = null;

    static DictationGrammar dictation;

    public static void main(String[] args) {
        try {

            RecognizerModeDesc desc = new RecognizerModeDesc(null, Boolean.TRUE);
            SpeechEngineChooser chooser = null;

            try {
                chooser = SpeechEngineChooser.getRecognizerDialog(desc);
                chooser.show();
            }
            catch (NoClassDefFoundError e) {
                System.out.println("Swing classes not found - continuing anyway");
            }
            if (chooser != null)
                desc = chooser.getRecognizerModeDesc();

            rec = Central.createRecognizer(desc);
            rec.addEngineListener(new TestEngineListener());

            System.out.println("STARTING TEST");

            RecognizerAudioAdapter raud = new TestAudioListener();
            CGAudioManager audioMan = (CGAudioManager) rec.getAudioManager();
            audioMan.addAudioListener(raud);

            audioMan.addTransferListener(new TransferListener() {
                public void bytesTransferred(TransferEvent evt) {
                    System.out.println("transferred " + evt.getLength());
                }
            });

            rec.allocate();
            rec.waitEngineState(Recognizer.ALLOCATED);

            dictation = rec.getDictationGrammar("dictation");
            dictation.setEnabled(true);
            // Set the TestResultListener to play back the audio and deallocate after one
            // recognition.
            dictation.addResultListener(new TestResultListener(rec, 1, true));
            RecognizerProperties props = rec.getRecognizerProperties();
            // Retain audio so it can be played back later (see TestResultListener)
            props.setResultAudioProvided(true);
            props.setNumResultAlternatives(4);

            System.out.println("Using engine " + rec.getEngineModeDesc());
            SpeakerManager speakerManager = rec.getSpeakerManager();
            if (chooser != null) {
                SpeakerProfile prof = chooser.getSpeakerProfile();
                speakerManager.setCurrentSpeaker(prof);
            }
            else {
                SpeakerProfile[] profs = speakerManager.listKnownSpeakers();
                speakerManager.setCurrentSpeaker(profs[0]);
            }

            System.out.println("Current Profile is " + speakerManager.getCurrentSpeaker());

            AudioFileSource source = new AudioFileSource(new File("resources\\hello_world.wav"));

            System.out.println("file fmt=" + source.getAudioFormat());
            System.out.println("rec fmt=" + audioMan.getAudioFormat());

            // convert to the recognizer audio format
            new AudioFormatConverter(source, audioMan, true);

            // need to use an AudioConverter as above - the following line used
            // in place of the above line will throw an Exception if the AudioManager
            // and source have different AudioFormats
            // audioMan.setSource(source);

            rec.commitChanges();
            rec.requestFocus();
            rec.waitEngineState(rec.LISTENING);

            source.startSending();
            System.out.println("sending");

            source.drain();
            System.out.println("drained");

            // deallocate after 10 seconds - in case nothing was recognized
            Thread killThread = new Thread() {
                public void run() {
                    try {
                        sleep(10000);
                        System.out.println("Given up waiting for an Accepted Result");
                        System.out.println("disabling dictation after audio data finished");
                        dictation.setEnabled(false);
                        if (!rec.testEngineState(rec.DEALLOCATED)
                            && !rec.testEngineState(rec.DEALLOCATING_RESOURCES)) {
                            rec.commitChanges();
                            rec.waitEngineState(rec.LISTENING);
                            sleep(5000);
                        }
                        System.out.println("Forcing finalize\n");
                        // forceFinalize causes a RESULT_ACCEPTED event to be sent, and
                        // the TestResultListener will deallocate the recognizer
                        rec.forceFinalize(true);
                        System.out.println("Forced finalize\n");
                        rec.deallocate();
                        System.out.println("deallocating\n");
                    }
                    catch (Exception e) {
                        e.printStackTrace();
                    }
                }
            };
            killThread.start();

            rec.waitEngineState(Recognizer.DEALLOCATED);
            // one recognition and the TestResultListener deallocates

            System.out.println("All done");

        }
        catch (Exception e) {
            e.printStackTrace(System.out);
        }
        catch (Error e1) {
            e1.printStackTrace(System.out);
        }
        finally {
            try {
                rec.deallocate();
            }
            catch (Exception e2) {
                e2.printStackTrace(System.out);
            }
            System.exit(0);
        }
    }
}

is it possible to recognize wav file from java is it possible with cloudgarden java speech api their is a sample code from cloudgarden example, any change is need for our own wav file
the sample code is below

import javax.speech.*;
import javax.speech.recognition.*;
import java.io.*;
import com.cloudgarden.audio.*;
import com.cloudgarden.speech.*;
import com.cloudgarden.speech.userinterface.SpeechEngineChooser;

/**
 * Tests running a dictation grammar against input from a wave file.
 */

public class DictationFromFile {
    static Recognizer rec = null;

    static DictationGrammar dictation;

    public static void main(String[] args) {
        try {

            RecognizerModeDesc desc = new RecognizerModeDesc(null, Boolean.TRUE);
            SpeechEngineChooser chooser = null;

            try {
                chooser = SpeechEngineChooser.getRecognizerDialog(desc);
                chooser.show();
            }
            catch (NoClassDefFoundError e) {
                System.out.println("Swing classes not found - continuing anyway");
            }
            if (chooser != null)
                desc = chooser.getRecognizerModeDesc();

            rec = Central.createRecognizer(desc);
            rec.addEngineListener(new TestEngineListener());

            System.out.println("STARTING TEST");

            RecognizerAudioAdapter raud = new TestAudioListener();
            CGAudioManager audioMan = (CGAudioManager) rec.getAudioManager();
            audioMan.addAudioListener(raud);

            audioMan.addTransferListener(new TransferListener() {
                public void bytesTransferred(TransferEvent evt) {
                    System.out.println("transferred " + evt.getLength());
                }
            });

            rec.allocate();
            rec.waitEngineState(Recognizer.ALLOCATED);

            dictation = rec.getDictationGrammar("dictation");
            dictation.setEnabled(true);
            // Set the TestResultListener to play back the audio and deallocate after one
            // recognition.
            dictation.addResultListener(new TestResultListener(rec, 1, true));
            RecognizerProperties props = rec.getRecognizerProperties();
            // Retain audio so it can be played back later (see TestResultListener)
            props.setResultAudioProvided(true);
            props.setNumResultAlternatives(4);

            System.out.println("Using engine " + rec.getEngineModeDesc());
            SpeakerManager speakerManager = rec.getSpeakerManager();
            if (chooser != null) {
                SpeakerProfile prof = chooser.getSpeakerProfile();
                speakerManager.setCurrentSpeaker(prof);
            }
            else {
                SpeakerProfile[] profs = speakerManager.listKnownSpeakers();
                speakerManager.setCurrentSpeaker(profs[0]);
            }

            System.out.println("Current Profile is " + speakerManager.getCurrentSpeaker());

            AudioFileSource source = new AudioFileSource(new File("resources\\hello_world.wav"));

            System.out.println("file fmt=" + source.getAudioFormat());
            System.out.println("rec fmt=" + audioMan.getAudioFormat());

            // convert to the recognizer audio format
            new AudioFormatConverter(source, audioMan, true);

            // need to use an AudioConverter as above - the following line used
            // in place of the above line will throw an Exception if the AudioManager
            // and source have different AudioFormats
            // audioMan.setSource(source);

            rec.commitChanges();
            rec.requestFocus();
            rec.waitEngineState(rec.LISTENING);

            source.startSending();
            System.out.println("sending");

            source.drain();
            System.out.println("drained");

            // deallocate after 10 seconds - in case nothing was recognized
            Thread killThread = new Thread() {
                public void run() {
                    try {
                        sleep(10000);
                        System.out.println("Given up waiting for an Accepted Result");
                        System.out.println("disabling dictation after audio data finished");
                        dictation.setEnabled(false);
                        if (!rec.testEngineState(rec.DEALLOCATED)
                            && !rec.testEngineState(rec.DEALLOCATING_RESOURCES)) {
                            rec.commitChanges();
                            rec.waitEngineState(rec.LISTENING);
                            sleep(5000);
                        }
                        System.out.println("Forcing finalize\n");
                        // forceFinalize causes a RESULT_ACCEPTED event to be sent, and
                        // the TestResultListener will deallocate the recognizer
                        rec.forceFinalize(true);
                        System.out.println("Forced finalize\n");
                        rec.deallocate();
                        System.out.println("deallocating\n");
                    }
                    catch (Exception e) {
                        e.printStackTrace();
                    }
                }
            };
            killThread.start();

            rec.waitEngineState(Recognizer.DEALLOCATED);
            // one recognition and the TestResultListener deallocates

            System.out.println("All done");

        }
        catch (Exception e) {
            e.printStackTrace(System.out);
        }
        catch (Error e1) {
            e1.printStackTrace(System.out);
        }
        finally {
            try {
                rec.deallocate();
            }
            catch (Exception e2) {
                e2.printStackTrace(System.out);
            }
            System.exit(0);
        }
    }
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

千里故人稀 2024-11-03 03:51:48

是的&不。

是的。 Java Speech API 提供了用于将文本转换为语音或将语音转换为文本(语音识别)的插件的挂钩。

不。遗憾的是,语音识别插件页面上列出的所有 API 链接要么已损坏,要么指向不提供插件(免费或 商业上)。对于在 Java Sound API 中进行语音识别的插件,似乎您需要自己编写它。

Yes & no.

Yes. The Java Speech API provides hooks for plug-ins that either convert text to speech, or speech to text (speech recognition).

No. Unfortunately all the links to APIs listed on the plug-ins page for speech recognition, are either broken or lead to places that do not offer a plug-in (free or commercially). For a plug-in to do speech recognition in the Java Sound API, it seems as though you would need to write it yourself.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文