Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

making it work

git-svn-id: svn://rubyforge.org/var/svn/daapclient/icha@130 d404eec9-990f-0410-b7b9-e628b63c8aec
  • Loading branch information...
commit 8521c03b5a06229096a11997cc5c914798f0464f 1 parent 26218ec
aaronp authored
View
17 README.txt
@@ -5,7 +5,8 @@
== DESCRIPTION. LULZ
Hai! icanhasaudio? is an interface to lame for decoding ur MP3s. I iz in ur
-computer. Decodin ur mp3s. Whatevs! I also decodin ur OGGz!
+computer. Decodin ur mp3s. Whatevs! I also decodin ur OGGz! I kin also
+encodin' ur WAV and AIFF to mp3z!
== SYNOPSYS ROFLOL
@@ -23,15 +24,27 @@ Or even smaller:
reader = Audio::OGG::Decoder.new
reader.decode(File.open(ARGV[0], 'rb'), File.open(ARGV[1], 'wb'))
+Encoder!!!!!! LOL
+
+ writer = Audio::MPEG::Encoder.new
+ File.open(ARGV[0]), 'rb') { |wav_lol|
+ File.open(ARGV[1]), 'wb+') { |mp3_lol|
+ writer.encode(wav_lol, mp3_lol)
+ }
+ }
+
== PROBLEMS
-Currently only decodes MP3/OGG data. Plus many other problems.... YMMV. LOL.
+Currently only decodes MP3/OGG data. Also encodes WAV/AIFF to mp3. Plus many
+other problems.... YMMV. LOL.
Not laugh plz!
== DEPENDENSEEZ
Make sure lame is installed on ur 'puter. Also ogg and vorbisfile!
+ # port install libvorbis vorbis-tools lame
+
== CREDITZ
Thanx Ryan for mah name! Also, most of this code was taken from the lame
View
123 ext/mpeg_encoder.c
@@ -25,6 +25,116 @@ encoder_allocate(VALUE klass) {
/*
* call-seq:
+ * encoder.vbr_hard_min=
+ *
+ * Strictly enforce the vbr min bitrate. Normally it will be violated for
+ * analog silence.
+ */
+static VALUE MpegEncoder_set_vbr_hard_min(VALUE self, VALUE boolean) {
+ lame_global_flags * gfp;
+
+ Data_Get_Struct(self, lame_global_flags, gfp);
+ lame_set_VBR_hard_min(gfp, boolean == Qtrue ? 1 : 0);
+ return boolean;
+}
+
+/*
+ * call-seq:
+ * encoder.vbr_hard_min?
+ *
+ * Get the hard minimum flag.
+ */
+static VALUE MpegEncoder_get_vbr_hard_min(VALUE self) {
+ lame_global_flags * gfp;
+
+ Data_Get_Struct(self, lame_global_flags, gfp);
+ return lame_get_VBR_hard_min(gfp) == 0 ? Qfalse : Qtrue;
+}
+
+/*
+ * call-seq:
+ * encoder.vbr_max_bitrate=
+ *
+ * Set the maximum vbr bitrate.
+ */
+static VALUE MpegEncoder_set_vbr_max_bitrate(VALUE self, VALUE brate) {
+ lame_global_flags * gfp;
+
+ Data_Get_Struct(self, lame_global_flags, gfp);
+ lame_set_VBR_max_bitrate_kbps(gfp, NUM2INT(brate));
+ return brate;
+}
+
+/*
+ * call-seq:
+ * encoder.vbr_max_bitrate
+ *
+ * Get the maximum vbr bitrate.
+ */
+static VALUE MpegEncoder_get_vbr_max_bitrate(VALUE self) {
+ lame_global_flags * gfp;
+
+ Data_Get_Struct(self, lame_global_flags, gfp);
+ return INT2NUM(lame_get_VBR_max_bitrate_kbps(gfp));
+}
+
+/*
+ * call-seq:
+ * encoder.vbr_min_bitrate=
+ *
+ * Set the minimum vbr bitrate.
+ */
+static VALUE MpegEncoder_set_vbr_min_bitrate(VALUE self, VALUE brate) {
+ lame_global_flags * gfp;
+
+ Data_Get_Struct(self, lame_global_flags, gfp);
+ lame_set_VBR_min_bitrate_kbps(gfp, NUM2INT(brate));
+ return brate;
+}
+
+/*
+ * call-seq:
+ * encoder.vbr_min_bitrate
+ *
+ * Get the minimum vbr bitrate.
+ */
+static VALUE MpegEncoder_get_vbr_min_bitrate(VALUE self) {
+ lame_global_flags * gfp;
+
+ Data_Get_Struct(self, lame_global_flags, gfp);
+ return INT2NUM(lame_get_VBR_min_bitrate_kbps(gfp));
+}
+
+/*
+ * call-seq:
+ * encoder.bitrate=
+ *
+ * Set the bitrate.
+ */
+static VALUE MpegEncoder_set_bitrate(VALUE self, VALUE brate) {
+ lame_global_flags * gfp;
+
+ Data_Get_Struct(self, lame_global_flags, gfp);
+ lame_set_brate(gfp, NUM2INT(brate));
+ lame_set_VBR_min_bitrate_kbps(gfp, lame_get_brate(gfp));
+ return brate;
+}
+
+/*
+ * call-seq:
+ * encoder.bitrate
+ *
+ * Get the bitrate.
+ */
+static VALUE MpegEncoder_get_bitrate(VALUE self) {
+ lame_global_flags * gfp;
+
+ Data_Get_Struct(self, lame_global_flags, gfp);
+ return INT2NUM(lame_get_brate(gfp));
+}
+
+/*
+ * call-seq:
* encoder.quality=
*
* Set the VBR quality. 0 = highest, 9 = lowest
@@ -368,10 +478,14 @@ void init_MpegEncoder(VALUE rb_mMpeg) {
rb_mAudio = rb_define_module("Audio");
rb_mMpeg = rb_define_module_under(rb_mAudio, "MPEG");
*/
+ /*
+ * Encode mp3s
+ */
cMpegEncoder = rb_define_class_under(rb_mMpeg, "Encoder", rb_cObject);
rb_define_alloc_func(cMpegEncoder, encoder_allocate);
/* Public Methods */
+
rb_define_method(cMpegEncoder, "vbr_quality=",MpegEncoder_set_vbr_quality, 1);
rb_define_method(cMpegEncoder, "vbr_quality", MpegEncoder_get_vbr_quality, 0);
rb_define_method(cMpegEncoder, "vbr_type=", MpegEncoder_set_vbr_type, 1);
@@ -383,6 +497,15 @@ void init_MpegEncoder(VALUE rb_mMpeg) {
rb_define_method(cMpegEncoder, "year=", MpegEncoder_set_year, 1);
rb_define_method(cMpegEncoder, "track=", MpegEncoder_set_track, 1);
rb_define_method(cMpegEncoder, "genre=", MpegEncoder_set_genre, 1);
+ rb_define_method(cMpegEncoder, "bitrate=", MpegEncoder_set_bitrate, 1);
+ rb_define_method(cMpegEncoder, "bitrate", MpegEncoder_get_bitrate, 0);
+ rb_define_method(cMpegEncoder, "vbr_min_bitrate=", MpegEncoder_set_vbr_min_bitrate, 1);
+ rb_define_method(cMpegEncoder, "vbr_min_bitrate", MpegEncoder_get_vbr_min_bitrate, 0);
+ rb_define_method(cMpegEncoder, "vbr_max_bitrate=", MpegEncoder_set_vbr_max_bitrate, 1);
+ rb_define_method(cMpegEncoder, "vbr_max_bitrate", MpegEncoder_get_vbr_max_bitrate, 0);
+ rb_define_method(cMpegEncoder, "vbr_hard_min=", MpegEncoder_set_vbr_hard_min, 1);
+ rb_define_method(cMpegEncoder, "vbr_hard_min?", MpegEncoder_get_vbr_hard_min, 0);
+
rb_define_private_method(cMpegEncoder, "init_params", MpegEncoder_init_params, 0);
rb_define_private_method(cMpegEncoder, "num_channels=", MpegEncoder_set_num_channels, 1);
View
7 lib/icanhasaudio/mpeg/encoder.rb
@@ -98,7 +98,8 @@ def parse_header(file)
when IFF_ID_FORM # AIFF file
parse_aiff_header(file)
else
- raise "Unsupported format"
+ $stderr.puts "Assuming RAW PCM"
+ file.rewind
end
end
@@ -157,7 +158,7 @@ def parse_aiff_header(file)
sub_size -= 4
sample_type = IFF_ID_SSND
- file.read(sub_size)
+ file.read(block_offset)
is_aiff = true
break
else
@@ -179,7 +180,7 @@ def parse_aiff_header(file)
@pcmbitwidth = sample_size
self.num_channels = num_channels
- self.in_samplerate = sample_rate
+ self.in_samplerate = sample_rate.to_i
self.num_samples = num_sample_frames
end
View
58 test/test_mpeg_encoder.rb
@@ -1,35 +1,67 @@
require 'test/unit'
require 'icanhasaudio'
+require 'tempfile'
class MPEGEncoderTest < Test::Unit::TestCase
+ include Audio::MPEG
+
WAV_FILE = File.dirname(__FILE__) + "/assets/testcase.wav"
- AIFF_FILE = File.dirname(__FILE__) + "/assets/cow.aiff"
+ #AIFF_FILE = File.dirname(__FILE__) + "/assets/cow.aiff"
def setup
@encoder = Audio::MPEG::Encoder.new
assert File.exists?(WAV_FILE)
+ #assert File.exists?(AIFF_FILE)
end
def test_encoder_initialize
assert Audio::MPEG::Encoder.new
end
- def test_encode_aiff
- File.open('/tmp/out_aiff.mp3', 'wb+') { |outfile|
- @encoder.encode(File.open(AIFF_FILE, 'rb'), outfile)
+ def test_encode
+ File.open("#{Dir::tmpdir}/out.mp3", 'wb+') { |outfile|
+ @encoder.encode(File.open(WAV_FILE, 'rb'), outfile)
}
end
- def test_encode
- File.open('/tmp/out.mp3', 'wb+') { |outfile|
+ def test_set_bitrate
+ @encoder.bitrate = 128
+ assert_equal(128, @encoder.bitrate)
+ assert_equal(128, @encoder.vbr_min_bitrate)
+ end
+
+ def test_vbr_type
+ @encoder.vbr_type = Encoder::VBR_OFF
+ assert_equal(Encoder::VBR_OFF, @encoder.vbr_type)
+ File.open("#{Dir::tmpdir}/no_vbr.mp3", 'wb+') { |outfile|
@encoder.encode(File.open(WAV_FILE, 'rb'), outfile)
}
end
+ def test_set_min_vbr_bitrate
+ @encoder.vbr_min_bitrate = 128
+ assert_equal(128, @encoder.vbr_min_bitrate)
+ end
+
+ def test_set_vbr_max_bitrate
+ @encoder.vbr_max_bitrate = 128
+ assert_equal(128, @encoder.vbr_max_bitrate)
+ @encoder.vbr_max_bitrate = 256
+ assert_equal(256, @encoder.vbr_max_bitrate)
+ end
+
+ def test_set_vbr_hard_min
+ assert_equal(false, @encoder.vbr_hard_min?)
+ @encoder.vbr_hard_min = true
+ assert_equal(true, @encoder.vbr_hard_min?)
+ @encoder.vbr_hard_min = false
+ assert_equal(false, @encoder.vbr_hard_min?)
+ end
+
def test_set_title
@encoder.title = 'tenderlovemaking.com'
- File.open('/tmp/title.mp3', 'wb+') { |outfile|
+ File.open("#{Dir::tmpdir}/title.mp3", 'wb+') { |outfile|
@encoder.encode(File.open(WAV_FILE, 'rb'), outfile)
}
end
@@ -37,7 +69,7 @@ def test_set_title
def test_set_artist
@encoder.artist = 'Aaron Patterson'
- File.open('/tmp/artist.mp3', 'wb+') { |outfile|
+ File.open("#{Dir::tmpdir}/artist.mp3", 'wb+') { |outfile|
@encoder.encode(File.open(WAV_FILE, 'rb'), outfile)
}
end
@@ -45,7 +77,7 @@ def test_set_artist
def test_set_album
@encoder.album = 'Some Album'
- File.open('/tmp/album.mp3', 'wb+') { |outfile|
+ File.open("#{Dir::tmpdir}/album.mp3", 'wb+') { |outfile|
@encoder.encode(File.open(WAV_FILE, 'rb'), outfile)
}
end
@@ -53,7 +85,7 @@ def test_set_album
def test_set_year
@encoder.year = 1999
- File.open('/tmp/year.mp3', 'wb+') { |outfile|
+ File.open("#{Dir::tmpdir}/year.mp3", 'wb+') { |outfile|
@encoder.encode(File.open(WAV_FILE, 'rb'), outfile)
}
end
@@ -61,7 +93,7 @@ def test_set_year
def test_set_track
@encoder.track = 1
- File.open('/tmp/track.mp3', 'wb+') { |outfile|
+ File.open("#{Dir::tmpdir}/track.mp3", 'wb+') { |outfile|
@encoder.encode(File.open(WAV_FILE, 'rb'), outfile)
}
end
@@ -69,7 +101,7 @@ def test_set_track
def test_set_genre
@encoder.genre = 'Porn Groove'
- File.open('/tmp/genre.mp3', 'wb+') { |outfile|
+ File.open("#{Dir::tmpdir}/genre.mp3", 'wb+') { |outfile|
@encoder.encode(File.open(WAV_FILE, 'rb'), outfile)
}
end
@@ -81,7 +113,7 @@ def test_set_all
@encoder.year = 2008
@encoder.track = 1
@encoder.genre = 'Rock'
- File.open('/tmp/all.mp3', 'wb+') { |outfile|
+ File.open("#{Dir::tmpdir}/all.mp3", 'wb+') { |outfile|
@encoder.encode(File.open(WAV_FILE, 'rb'), outfile)
}
end
Please sign in to comment.
Something went wrong with that request. Please try again.