Clojure Cookbook: System

Determining Clojure Version


How do I tell which version of Clojure I am using?


Evaluate the variable clojure.core/*clojure-version* or the function clojure.core/clojure-version.

(doc *clojure-version*)
  The version info for Clojure core, as a map containing :major :minor
  :incremental and :qualifier keys. Feature releases may increment
  :minor and/or :major, bugfix releases will increment :incremental.
  Possible values of :qualifier include "GA", "SNAPSHOT", "RC-x" "BETA-x"
(doc clojure-version)
  Returns clojure version as a printable string.

Determining Number of Available Processors


How do I tell how many processors my computer has?


Ask the Java runtime system:

(.availableProcessors (Runtime/getRuntime)) => 2

Screen Capture


How do I do a screen capture using Clojure?


Use the createScreenCapture() method of java.awt.Robot:

(import '(java.awt AWTException Robot Rectangle Toolkit)
        '(java.awt.image BufferedImage)
        '( File IOException)
        '(javax.imageio ImageIO))
(defn screen-grab [file-name]
  (let [img-type (second (re-find (re-matcher #"\.(\w+)$" file-name)))
        capture (.createScreenCapture (Robot.)
                                      (Rectangle. (.getScreenSize (Toolkit/getDefaultToolkit))))
        file (File. file-name)]
    (ImageIO/write capture img-type file)))

The variable capture is an instance of java.awt.image.BufferedImage, and ImageIO.write() outputs it as a file, typically either as JPEG/JPG or PNG.

Playing a Sound File


How do I play a sound file from my program?


The simple way to play a .wav or MIDI file is to use the method Applet.newAudioClip() to create a java.applet.AudioClip instance and call its play() method:

(import '(java.applet Applet)
        '( File)
    '( URL))
(defn play-url [url-string]
  (.play (Applet/newAudioClip (URL. url-string))))

(defn play-file [file-name]
  (let [absolute-name (.getAbsolutePath (File. file-name))
        url-string (str "file://" absolute-name)]
    (play-url url-string)))

The newAudioClip() method expects a instance, so for local files we have to add the prefix "file://".

(play-url "")
(play-file "beanears.mid")

Back to Clojure Cookbook: Table of Contents


Add a New Comment
or Sign in as Wikidot user
(will not be published)
- +

Unless otherwise stated, the content of this page is licensed under Creative Commons Attribution-ShareAlike 3.0 License