Skip to content

Commit

Permalink
Add support for writing to the sound using "<<"
Browse files Browse the repository at this point in the history
  • Loading branch information
warhammerkid committed Mar 15, 2010
1 parent 9ba9448 commit 6e6dcff
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 0 deletions.
14 changes: 14 additions & 0 deletions ext/ra_sound.c
Expand Up @@ -16,6 +16,7 @@ void Init_ra_sound() {
rb_define_method(cRASound, "seek", ra_sound_seek, 2);
rb_define_method(cRASound, "read", ra_sound_read, 2);
rb_define_method(cRASound, "write", ra_sound_write, 1);
rb_define_method(cRASound, "<<", ra_sound_addbuf, 1);
rb_define_method(cRASound, "close", ra_sound_close, 0);
rb_define_method(cRASound, "closed?", ra_sound_closed, 0);
}
Expand Down Expand Up @@ -223,6 +224,19 @@ static VALUE ra_sound_write(VALUE self, VALUE buf) {
return OFFT2NUM(written);
}

/*
* call-seq:
* snd << buf => snd
*
* Writes the given buffer to the string.
*
* snd << buf1 << buf2
*/
static VALUE ra_sound_addbuf(VALUE self, VALUE buf) {
ra_sound_write(self, buf);
return self;
}

/*
* call-seq:
* snd.close => nil
Expand Down
1 change: 1 addition & 0 deletions ext/ra_sound.h
Expand Up @@ -29,6 +29,7 @@ static VALUE ra_sound_info(VALUE self);
static VALUE ra_sound_seek(VALUE self, VALUE frames, VALUE whence);
static VALUE ra_sound_read(VALUE self, VALUE buf, VALUE frames);
static VALUE ra_sound_write(VALUE self, VALUE buf);
static VALUE ra_sound_addbuf(VALUE self, VALUE buf);
static VALUE ra_sound_close(VALUE self);
static VALUE ra_sound_close_safe(VALUE self);
static VALUE ra_sound_closed(VALUE self);
Expand Down
18 changes: 18 additions & 0 deletions spec/sound_spec.rb
Expand Up @@ -126,4 +126,22 @@

out_buf[50].should == in_buf[50]
end

it "should allow writing to a new sound using <<" do
in_buf = RubyAudio::Buffer.float(100)
out_buf = RubyAudio::Buffer.float(100)
out_info = nil
RubyAudio::Sound.open(MONO_TEST_WAV) do |snd|
snd.read(in_buf)
out_info = snd.info.clone
end

RubyAudio::Sound.open(OUT_WAV, 'rw', out_info) do |snd|
snd << in_buf
snd.seek(0)
snd.read(out_buf)
end

out_buf[50].should == in_buf[50]
end
end

0 comments on commit 6e6dcff

Please sign in to comment.