I need to read WAV files, and don't have time to recode the reader in
Clojure.  It's Java, and it's got side effects.  I can read a number
of frames from the WAV file with this code:

(defn read-frames [wav-file num-frames]
  (let [num-samples (* num-frames (.getNumChannels wav-file))
        buffer (double-array num-samples)
        frames-read (.readFrames wav-file buffer 0 num-frames)]
    (if (= frames-read num-frames)
      (vec buffer)
      nil)))

So this is pretty straightforward...it uses this wav-file object,
calls .readFrames() on it which reads into an array of doubles (passed
in as buffer) and returns a vector of these values.  Every time you
call this function, it returns the next num-frames values as a vector,
so it's operating by side effects.

I want to make a seq of reading these frames.  I thought I might use
repeatedly, but it doesn't know to stop when the f returns nil.  So I
just coded it myself:

(defn wavfile-chunk-seq [num-frames wav-file]
  (lazy-seq (let [x (read-frames wav-file num-frames)]
              (if (nil? x)
                '()
                (cons x (wavfile-chunk-seq num-frames wav-file))))))

Only problem is, it's holding onto its head somehow.  When I (count
(wavfile-chunk-seq n wav-file)) and check memory, it's all still there
somehow.  I'm 99% positive the WAV file object in Java isn't retaining
the memory, because it uses a much smaller buffer.

Can anybody see what I'm doing wrong?  Maybe I should change my
approach, and try to generate a Java Iterator over these values, and
just use iterator-seq?

Thanks in advance for any ideas...
Mike

-- 
You received this message because you are subscribed to the Google
Groups "Clojure" group.
To post to this group, send email to clojure@googlegroups.com
Note that posts from new members are moderated - please be patient with your 
first post.
To unsubscribe from this group, send email to
clojure+unsubscr...@googlegroups.com
For more options, visit this group at
http://groups.google.com/group/clojure?hl=en

Reply via email to