New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

java.io.FileNotFoundException: AudioTestNow.flac (The requested operation cannot be performed on a file with a user-mapped section open) #56

Closed
AdamuKaapan opened this Issue Dec 24, 2014 · 4 comments

Comments

Projects
None yet
3 participants
@AdamuKaapan
Contributor

AdamuKaapan commented Dec 24, 2014

When following this tutorial that you have written, but slightly modified to use the V2 speech API (GSpeechDuplex) instead of the V1 API shown in the tutorial, I get this error: java.io.FileNotFoundException: AudioTestNow.flac (The requested operation cannot be performed on a file with a user-mapped section open)
No line is given that is inside my code. However, it still gets a response from the server (and is correct).

The modified code from the tutorial:

GSpeechDuplex dup = new GSpeechDuplex(API_KEY);
dup.addResponseListener((GoogleResponse gr) ->
{
    displayResponse(gr);
});
dup.recognize(mic.getAudioFile(), (int)mic.getAudioFormat().getSampleRate());

In case you want it, here's the full log:

java.io.FileNotFoundException: AudioTestNow.flac (The requested operation cannot be performed on a file with a user-mapped section open)
at java.io.FileOutputStream.open(Native Method)
at java.io.FileOutputStream.(FileOutputStream.java:213)
at java.io.FileOutputStream.(FileOutputStream.java:162)
at javaFlacEncoder.FLACFileOutputStream.(FLACFileOutputStream.java:53)
at javaFlacEncoder.FLACFileWriter.write(FLACFileWriter.java:102)
at javax.sound.sampled.AudioSystem.write(AudioSystem.java:1356)
at com.darkprograms.speech.microphone.Microphone$CaptureThread.run(Microphone.java:228)
at java.lang.Thread.run(Thread.java:745)

Let me know if you need any more information.

@Skylion007

This comment has been minimized.

Show comment
Hide comment
@Skylion007

Skylion007 Jan 6, 2015

Collaborator

Oh sorry about that, the glitch has to do with the fact that the OS can sometimes take a few seconds to close the file from before. The easiest way to fix this is just to read the file using a non-blocking method. If you are using Java 7 then the easiest built in method would be Files.readAllBytes() instead of the custom one I provided to read the bytes of the File.

Hope this helps and Happy New Year!

Collaborator

Skylion007 commented Jan 6, 2015

Oh sorry about that, the glitch has to do with the fact that the OS can sometimes take a few seconds to close the file from before. The easiest way to fix this is just to read the file using a non-blocking method. If you are using Java 7 then the easiest built in method would be Files.readAllBytes() instead of the custom one I provided to read the bytes of the File.

Hope this helps and Happy New Year!

@AdamuKaapan

This comment has been minimized.

Show comment
Hide comment
@AdamuKaapan

AdamuKaapan Jan 7, 2015

Contributor

Alright, thanks. That makes sense, actually.

Contributor

AdamuKaapan commented Jan 7, 2015

Alright, thanks. That makes sense, actually.

@davidfer1991

This comment has been minimized.

Show comment
Hide comment
@davidfer1991

davidfer1991 Jan 20, 2015

Hello,

What else did you change in order to get an answer from google?

For example, what do i have to change in this line:

private static final String GOOGLE_RECOGNIZER_URL = "https://www.google.com/speech-api/v1/recognize?xjerr=1&client=chromium";

Thanks in advance..

davidfer1991 commented Jan 20, 2015

Hello,

What else did you change in order to get an answer from google?

For example, what do i have to change in this line:

private static final String GOOGLE_RECOGNIZER_URL = "https://www.google.com/speech-api/v1/recognize?xjerr=1&client=chromium";

Thanks in advance..

@AdamuKaapan

This comment has been minimized.

Show comment
Hide comment
@AdamuKaapan

AdamuKaapan Jan 20, 2015

Contributor

I THINK that the code that I posted is all that I changed: I'll post all of the applicable code, for what I have that seems to work decently.

public  void ambientListeningLoop()
{
    MicrophoneAnalyzer mic = new MicrophoneAnalyzer(FLACFileWriter.FLAC);
//  mic.setAudioFile(new File("AudioTestNow.flac"));
    int file = -1;
    while (true)
    {
        file++;
        mic.setAudioFile(new File("AudioTest" + file + ".flac"));
        mic.open();
        final int THRESHOLD = mic.getAudioVolume();
        int volume = mic.getAudioVolume();
        boolean isSpeaking = (volume > THRESHOLD);
        if (isSpeaking)
        {
            try
            {
                System.out.println("RECORDING...");
                mic.captureAudioToFile(mic.getAudioFile());//Saves audio to file.

                do
                {
                    Thread.sleep(1000);//Updates every second
                }
                while (mic.getAudioVolume() > THRESHOLD);

                System.out.println("Recording Complete!");
                System.out.println("Recognizing...");

                GSpeechDuplex dup = new GSpeechDuplex(API_KEY);
                dup.addResponseListener((GoogleResponse gr) ->
                {
                    displayResponse(gr);
                });

                dup.recognize(mic.getAudioFile(), (int)mic.getAudioFormat().getSampleRate());

                System.out.println("Looping back");
            }
            catch (Exception e)
            {
                System.out.println("Error Occured");
                e.printStackTrace();
            }
            finally
            {
                System.out.println("I'm hitting the finally!");
                mic.close();
            }
        }
    }
}

So, there's a few extra modifications in there, but you should be able to figure out what I changed from the original. As for what you did, I'm not quite sure what that's referring to... I'm just using the pre-made GSpeechDuplex class, with some extra changes, I think. It's been a while since I worked on this, but I could try and figure it out again with more explanation, if you wanted.

Hope that helps!

Contributor

AdamuKaapan commented Jan 20, 2015

I THINK that the code that I posted is all that I changed: I'll post all of the applicable code, for what I have that seems to work decently.

public  void ambientListeningLoop()
{
    MicrophoneAnalyzer mic = new MicrophoneAnalyzer(FLACFileWriter.FLAC);
//  mic.setAudioFile(new File("AudioTestNow.flac"));
    int file = -1;
    while (true)
    {
        file++;
        mic.setAudioFile(new File("AudioTest" + file + ".flac"));
        mic.open();
        final int THRESHOLD = mic.getAudioVolume();
        int volume = mic.getAudioVolume();
        boolean isSpeaking = (volume > THRESHOLD);
        if (isSpeaking)
        {
            try
            {
                System.out.println("RECORDING...");
                mic.captureAudioToFile(mic.getAudioFile());//Saves audio to file.

                do
                {
                    Thread.sleep(1000);//Updates every second
                }
                while (mic.getAudioVolume() > THRESHOLD);

                System.out.println("Recording Complete!");
                System.out.println("Recognizing...");

                GSpeechDuplex dup = new GSpeechDuplex(API_KEY);
                dup.addResponseListener((GoogleResponse gr) ->
                {
                    displayResponse(gr);
                });

                dup.recognize(mic.getAudioFile(), (int)mic.getAudioFormat().getSampleRate());

                System.out.println("Looping back");
            }
            catch (Exception e)
            {
                System.out.println("Error Occured");
                e.printStackTrace();
            }
            finally
            {
                System.out.println("I'm hitting the finally!");
                mic.close();
            }
        }
    }
}

So, there's a few extra modifications in there, but you should be able to figure out what I changed from the original. As for what you did, I'm not quite sure what that's referring to... I'm just using the pre-made GSpeechDuplex class, with some extra changes, I think. It's been a while since I worked on this, but I could try and figure it out again with more explanation, if you wanted.

Hope that helps!

@Skylion007 Skylion007 closed this Feb 20, 2015

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment