# Reading and writing narray/sound data to file

**URL:** <https://www.ruby-forum.com/t/reading-and-writing-narray-sound-data-to-file/160500>\
**Category:** Ruby\
**Created:** [February 17, 2009, 2:57am UTC](https://www.ruby-forum.com/t/reading-and-writing-narray-sound-data-to-file/160500 "2009-02-17T02:57:15Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![jbhruby](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/439d5e/32.png) [@jbhruby](https://www.ruby-forum.com/u/jbhruby)\
**Post date:** [February 17, 2009, 2:57am UTC](https://www.ruby-forum.com/t/reading-and-writing-narray-sound-data-to-file/160500/1 "2009-02-17T02:57:15Z")

</div>

Hi,

I was wondering if anyone could help.

I’m writing a program that analyses and processes audio in Ruby, using  
the audio/sndfile library and narray. Currently I’m analysing a file and  
storing the analysis data, an Audio::Sound class, directly into a file  
like so:

```
sf = Audio::Soundfile.open(self.full_filename)
an_file = sf.readf_float(sf.frames)
File.open(self.full_filename + ".anl","w") do |out|
  out << an_file
end

```

So now I have the data in this file stored directly as it is stored in  
the an\_file NArray.

My problem is that I want to crack open this file and load it directly  
into a an Audio::Sound NArray. File.read and IO.read seem to load the  
data as one giant string, even if I’ve previously declared that the  
variable i’m loading it into is an Audio::Sound. Is there no way to  
simply dump the data as-is?

Thanks

---

<div class="post-metadata">

**Author:** ![jbhruby](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/439d5e/32.png) [@jbhruby](https://www.ruby-forum.com/u/jbhruby)\
**Post date:** [February 17, 2009, 4:13am UTC](https://www.ruby-forum.com/t/reading-and-writing-narray-sound-data-to-file/160500/2 "2009-02-17T04:13:11Z")

</div>

Having been able to declutter my brain I’m guessing I maybe should use  
the Marshal module to serialize the data before saving, then use Marshal  
to load it in situ?
