1

I am using CMU sphinx, i am running my program but getting this error appears "Can't open microphone line with format PCM_SIGNED 16000.0 Hz, 16 bit, mono, 2 bytes/frame, big-endian not supported." Cannot start microphone. i want my program to listen voice using JButton whenever JButton clicked but i can only execute once in single button click. Next click will error as i stated above. please help me what is wrong.

This is what i am doing.

public class VoiceChat {

private static JPanel contentPane;
private static JTextField textField;
private static JTextField textField2;
private static JWindow window;
private static boolean alive = true;
private static String chatMessage;
private static boolean runTimer = true;
private static boolean OverSession = false;
private static Recognizer recognizer;
private static Microphone microphone;

public VoiceChat() {
    ExecutorService executor = Executors.newFixedThreadPool(2);

    FutureTask<String> CountTimer = new FutureTask<String>(
            new ShowCounter());
    FutureTask<String> talkMode = new FutureTask<String>(new DoTalk());

    executor.submit(CountTimer);
    initRecognize();
    executor.submit(talkMode);

    try {
        System.out.println(talkMode.get(20, TimeUnit.SECONDS));
    } catch (InterruptedException e) {
        e.printStackTrace();
    } catch (ExecutionException e) {
        e.printStackTrace();
    } catch (TimeoutException e) {
        executor.shutdownNow();
        e.printStackTrace();
    } finally {
        executor.shutdown();
    }
}

public static void initGUI()
{
    window = new JWindow();
    window.setBounds(100, 100, 400, 200);
    window.setLocationRelativeTo(null);
    window.setOpacity(0.9f);
    window.setVisible(true);

    textField = new JTextField();
    textField.setVisible(true);
    textField.setEnabled(false);
    textField.setBorder(new LineBorder(new Color(139, 0, 139), 2));
    textField.setFont(new Font("Tunga", Font.BOLD, 36));
    textField.setBounds(138, 83, 100, 53);
    window.add(textField);
    textField.setColumns(10);

    JLabel lblNewLabel = new JLabel("Start Talking in 10 Seconds");
    lblNewLabel.setVisible(true);
    lblNewLabel.setForeground(new Color(0, 0, 255));
    lblNewLabel.setFont(new Font("Batang", Font.PLAIN, 14));
    lblNewLabel.setBounds(90, 83, 100, 53);
    window.add(lblNewLabel);
}

public static void initRecognize()
{
    ConfigurationManager cm = new ConfigurationManager(
            VoiceChat.class.getResource("myconfig.xml"));
            recognizer = (Recognizer) cm.lookup("recognizer");
            recognizer.allocate();
            microphone = (Microphone) cm.lookup("microphone");
            if (!microphone.startRecording()) {
                System.out.println("Cannot start microphone.");
                recognizer.deallocate();
                microphone.stopRecording();
            }
}


public static boolean isAlive() {
    return alive;
}

public static void setAlive(boolean alive) {
    VoiceChat.alive = alive;
}

public static boolean isRunTimer() {
    return runTimer;
}

public static void setRunTimer(boolean runTimer) {
    VoiceChat.runTimer = runTimer;
}

public static boolean isOverSession() {
    return OverSession;
}

public static void setOverSession(boolean overSession) {
    OverSession = overSession;
}

public static String getChatMessage() {
    return chatMessage;
}

public static void setChatMessage(String chatMessage) {
    VoiceChat.chatMessage = chatMessage;
}

static class ShowCounter implements Callable<String> {

    @Override
    public String call() throws Exception {
        Thread.sleep(5000);
        Integer timeNote = 0;
        initGUI();
        try {
            for (int i = 0; i < 10; i++) {
                if (!isRunTimer()) {
                    textField.setVisible(false);
                    window.setVisible(false);
                    window.dispose();
                    break;
                }
                textField.setText("     " + (10 - i) + "");
                timeNote = 10 - (10 - i);
                Thread.sleep(1000);
            }
        } catch (InterruptedException e) {
            e.printStackTrace();
        }

        textField.setVisible(false);
        window.setVisible(false);
        window.dispose();
        setAlive(false);
        setOverSession(true);
        return timeNote.toString();
    }

}

static class DoTalk implements Callable<String> {

    @Override
    public String call() throws Exception {

        String resultText = "";

        System.out.println("Start speaking. Press Ctrl-C to quit.\n");

        while (isAlive()) {

            Result result = recognizer.recognize();

            if (result != null) {
                resultText = result.getBestFinalResultNoFiller();
                System.out.println((new StringBuilder())
                        .append("You said: ").append(resultText)
                        .append('\n').toString());

                if (!resultText.isEmpty()) {
                    setRunTimer(false);
                    break;
                }

                // wordFilter.add(resultText);
            } else {
                System.out.println("I can't hear what you said.\n");
            }
        }

        microphone.stopRecording();

        if (recognizer.getState() == State.ALLOCATED) {
            recognizer.deallocate();
        }

        setChatMessage(resultText);

        return resultText;

    }

    public static void main(String[] args) {

        JFrame frames = new JFrame();
        frames.setTitle("Simple MultiThread");
        frames.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frames.setBounds(100, 100, 450, 300);
        frames.setResizable(false);
        frames.setVisible(true);
        contentPane = new JPanel();
        contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
        contentPane.setLayout(null);
        frames.setContentPane(contentPane);
        JButton btnStart = new JButton("Start");
          btnStart.addActionListener(new ActionListener() {
           public void actionPerformed(ActionEvent arg0) {
               VoiceChat x = new VoiceChat();
               System.out.println(getChatMessage());    
          }});
          btnStart.setFont(new Font("Mangal", Font.BOLD, 16));
          btnStart.setBounds(180, 166, 78, 23);
          contentPane.add(btnStart);

    }
}}    

Thanks so much. Hope you can help me.

Hovercraft Full Of Eels
  • 283,665
  • 25
  • 256
  • 373
davinma06
  • 23
  • 4
  • 2
    I don't use Sphinx, but I wonder if your original speaker connection has fully closed when you try to open up a new one on the 2nd press of your button. On an unrelated note, I do code Swing however, and your code breaks all Swing threading rules. While this is not the cause of your current problem, it's a setup for your future problems. Another unrelated problem is your over-use of static fields which will make your code difficult to improve on or test. – Hovercraft Full Of Eels Oct 26 '14 at 16:42

0 Answers0