Add extension "vorbis_file"
This commit is contained in:
parent
08460f4a57
commit
c238308b53
|
@ -44,6 +44,7 @@
|
|||
# Rake-compiled extension files:
|
||||
/lib/tox/tox.so
|
||||
/lib/opus_file.so
|
||||
/lib/vorbis_file.so
|
||||
|
||||
# Vendored dependencies:
|
||||
/vendor/*
|
||||
|
|
4
Rakefile
4
Rakefile
|
@ -59,6 +59,10 @@ begin
|
|||
Rake::ExtensionTask.new 'opus_file' do |ext|
|
||||
ext.lib_dir = File.expand_path('lib', __dir__).freeze
|
||||
end
|
||||
|
||||
Rake::ExtensionTask.new 'vorbis_file' do |ext|
|
||||
ext.lib_dir = File.expand_path('lib', __dir__).freeze
|
||||
end
|
||||
rescue LoadError
|
||||
nil
|
||||
end
|
||||
|
|
|
@ -5,6 +5,7 @@ require 'bundler/setup'
|
|||
|
||||
require 'tox'
|
||||
require 'opus_file'
|
||||
require 'vorbis_file'
|
||||
|
||||
# You can add fixtures and/or initialization code here to make experimenting
|
||||
# with your gem easier. You can also use a different console, if you like.
|
||||
|
|
|
@ -8,7 +8,7 @@ rvm:
|
|||
|
||||
before_install:
|
||||
- sudo apt-get update
|
||||
- sudo apt-get install -y yasm libconfig-dev libopus-dev libopusfile-dev
|
||||
- sudo apt-get install -y yasm libconfig-dev libopus-dev libopusfile-dev libvorbis-dev
|
||||
|
||||
install: bundle install --jobs 3 --retry 3 --without examples
|
||||
|
||||
|
@ -19,3 +19,4 @@ before_script:
|
|||
|
||||
- rake compile:tox
|
||||
- rake compile:opus_file
|
||||
- rake compile:vorbis_file
|
||||
|
|
|
@ -0,0 +1,35 @@
|
|||
#!/usr/bin/env ruby
|
||||
# frozen_string_literal: true
|
||||
|
||||
require 'mkmf'
|
||||
|
||||
def cflags(*args)
|
||||
args.each do |str|
|
||||
$CFLAGS += " #{str.shellescape} "
|
||||
end
|
||||
end
|
||||
|
||||
def pkg_config!(*args)
|
||||
exit 1 unless pkg_config(*args)
|
||||
end
|
||||
|
||||
def have_library!(*args)
|
||||
exit 1 unless have_library(*args)
|
||||
end
|
||||
|
||||
def have_header!(*args)
|
||||
exit 1 unless have_header(*args)
|
||||
end
|
||||
|
||||
cflags '-std=c11'
|
||||
cflags '-Wall'
|
||||
cflags '-Wextra'
|
||||
cflags '-Wno-declaration-after-statement'
|
||||
|
||||
pkg_config! 'vorbisfile'
|
||||
|
||||
have_library! 'vorbisfile'
|
||||
|
||||
have_header! 'vorbis/vorbisfile.h'
|
||||
|
||||
create_makefile 'vorbis_file' or exit 1
|
|
@ -0,0 +1,69 @@
|
|||
#include <ruby.h>
|
||||
#include <stdbool.h>
|
||||
#include <vorbis/vorbisfile.h>
|
||||
|
||||
void Init_vorbis_file();
|
||||
|
||||
struct rb_cVorbisFile_CDATA {
|
||||
bool opened;
|
||||
OggVorbis_File ogg_vorbis_file;
|
||||
};
|
||||
|
||||
static VALUE rb_cVorbisFile = 0;
|
||||
|
||||
static VALUE rb_cVorbisFile_alloc(VALUE klass);
|
||||
static void rb_cVorbisFile_free(struct rb_cVorbisFile_CDATA *vorbis_file_cdata);
|
||||
|
||||
static VALUE rb_cVorbisFile_initialize(VALUE vorbis_file, VALUE filename);
|
||||
|
||||
void Init_vorbis_file()
|
||||
{
|
||||
rb_cVorbisFile = rb_define_class("VorbisFile", rb_cObject);
|
||||
|
||||
rb_define_alloc_func(rb_cVorbisFile, rb_cVorbisFile_alloc);
|
||||
|
||||
rb_define_method(rb_cVorbisFile, "initialize", rb_cVorbisFile_initialize, 1);
|
||||
}
|
||||
|
||||
VALUE rb_cVorbisFile_alloc(const VALUE klass)
|
||||
{
|
||||
struct rb_cVorbisFile_CDATA *const vorbis_file_cdata =
|
||||
ALLOC(struct rb_cVorbisFile_CDATA);
|
||||
|
||||
vorbis_file_cdata->opened = false;
|
||||
|
||||
return Data_Wrap_Struct(klass, NULL, rb_cVorbisFile_free, vorbis_file_cdata);
|
||||
}
|
||||
|
||||
void rb_cVorbisFile_free(struct rb_cVorbisFile_CDATA *const vorbis_file_cdata)
|
||||
{
|
||||
if (vorbis_file_cdata->opened) {
|
||||
ov_clear(&vorbis_file_cdata->ogg_vorbis_file);
|
||||
}
|
||||
|
||||
free(vorbis_file_cdata);
|
||||
}
|
||||
|
||||
VALUE rb_cVorbisFile_initialize(
|
||||
const VALUE vorbis_file,
|
||||
/* const */ VALUE filename
|
||||
)
|
||||
{
|
||||
struct rb_cVorbisFile_CDATA *vorbis_file_cdata = NULL;
|
||||
|
||||
const char *filename_data = StringValueCStr(filename);
|
||||
|
||||
Data_Get_Struct(vorbis_file, struct rb_cVorbisFile_CDATA, vorbis_file_cdata);
|
||||
|
||||
if (vorbis_file_cdata->opened) {
|
||||
rb_raise(rb_eRuntimeError, "already initialized");
|
||||
}
|
||||
|
||||
if (ov_fopen(filename_data, &vorbis_file_cdata->ogg_vorbis_file)) {
|
||||
rb_raise(rb_eRuntimeError, "can not open file \"%s\"", filename_data);
|
||||
}
|
||||
|
||||
vorbis_file_cdata->opened = true;
|
||||
|
||||
return vorbis_file;
|
||||
}
|
Binary file not shown.
|
@ -0,0 +1,35 @@
|
|||
# frozen_string_literal: true
|
||||
|
||||
RSpec.describe VorbisFile do
|
||||
subject { described_class.new filename }
|
||||
|
||||
let :filename do
|
||||
File.expand_path('../../multimedia/test_vorbis.ogg', __dir__).freeze
|
||||
end
|
||||
|
||||
describe '#initialize' do
|
||||
context 'when filename is not a string' do
|
||||
let(:filename) { 123 }
|
||||
|
||||
specify do
|
||||
expect { subject }.to raise_error TypeError
|
||||
end
|
||||
end
|
||||
|
||||
context 'when filename contains null bytes' do
|
||||
let(:filename) { "foo\x00bar" }
|
||||
|
||||
specify do
|
||||
expect { subject }.to raise_error ArgumentError
|
||||
end
|
||||
end
|
||||
|
||||
context 'when file is not a valid Ogg/Vorbis file' do
|
||||
let(:filename) { __FILE__ }
|
||||
|
||||
specify do
|
||||
expect { subject }.to raise_error RuntimeError
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
|
@ -27,6 +27,7 @@ require 'faker'
|
|||
|
||||
require 'tox'
|
||||
require 'opus_file'
|
||||
require 'vorbis_file'
|
||||
|
||||
require 'support/random_port'
|
||||
|
||||
|
|
|
@ -39,6 +39,7 @@ Gem::Specification.new do |spec|
|
|||
|
||||
spec.extensions << 'ext/tox/extconf.rb'
|
||||
spec.extensions << 'ext/opus_file/extconf.rb'
|
||||
spec.extensions << 'ext/vorbis_file/extconf.rb'
|
||||
|
||||
spec.add_development_dependency 'bundler', '~> 1.13'
|
||||
spec.add_development_dependency 'celluloid', '~> 0.17'
|
||||
|
|
Reference in New Issue