Question

Is it possible to cast in clojure with java style? This is java code which I want to implement in clojure:

public class JavaSoundRecorder {
    // the line from which audio data is captured
    TargetDataLine line;
    /**
     * Captures the sound and record into a WAV file
     */
    void start() {
        try {
            AudioFormat format = new AudioFormat(16000, 8,
                2, true, true);
            DataLine.Info info = new DataLine.Info(TargetDataLine.class, format);
            System.out.println(AudioSystem.isLineSupported(info));
            // 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 = AudioSystem.getTargetDataLine(format);
            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, AudioFileFormat.Type.WAVE, new File("RecordAudio.wav"));

        } catch (LineUnavailableException ex) {
            ex.printStackTrace();
        } catch (IOException ioe) {
            ioe.printStackTrace();
        }
    }

    /**
     * Closes the target data line to finish capturing and recording
     */
    void finish() {
        line.stop();
        line.close();
        System.out.println("Finished");
    }

    /**
     * Entry to run the program
     */
    public static void main(String[] args) {
        final JavaSoundRecorder recorder = new JavaSoundRecorder();

        // creates a new thread that waits for a specified
        // of time before stopping
        Thread stopper = new Thread(new Runnable() {
            public void run() {
                try {
                    Thread.sleep(6000);
                } catch (InterruptedException ex) {
                    ex.printStackTrace();
                }
                recorder.finish();
            }
        });

        stopper.start();

        // start recording
        recorder.start();
    }
}

And this is what I made in clojure

    (def audioformat (new javax.sound.sampled.AudioFormat 16000 8 2 true true))
    (def info (new javax.sound.sampled.DataLine$Info javax.sound.sampled.TargetDataLine audioformat))
    (if (not= (javax.sound.sampled.AudioSystem/isLineSupported info))(print "dataline not supported")(print "ok lets start\n"))
    (def line (javax.sound.sampled.AudioSystem/getTargetDataLine audioformat))
    (.open line audioformat)

are there any solutions?

Was it helpful?

Solution

this issue was explained rather well on the Clojure group here:

https://groups.google.com/forum/#!topic/clojure/SNcT6d-TTaQ

You should not need to do the cast (see the discussion in the comments about the super types of the object we have), however you will need to type hint the invocation of open:

(.open ^javax.sound.sampled.TargetDataLine line audioformat)

Remember that java casts don't really do very much (not like C++ where a cast might completely transform an underlying object).

I am not sure what this code is supposed to do, so I don't know whether it has worked or not. Certainly, I can now run your example without error.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top