toxon
/
tox.rb
Archived
1
0
Fork 0

Add extension "vorbis_file"

This commit is contained in:
Braiden Vasco 2018-05-01 09:55:37 +00:00
parent 08460f4a57
commit c238308b53
No known key found for this signature in database
GPG Key ID: F1D3CA63437BDC6F
10 changed files with 149 additions and 1 deletions

1
.gitignore vendored
View File

@ -44,6 +44,7 @@
# Rake-compiled extension files:
/lib/tox/tox.so
/lib/opus_file.so
/lib/vorbis_file.so
# Vendored dependencies:
/vendor/*

View File

@ -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

View File

@ -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.

View File

@ -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

35
ext/vorbis_file/extconf.rb Executable file
View 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

69
ext/vorbis_file/main.c Normal file
View File

@ -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;
}

BIN
multimedia/test_vorbis.ogg Normal file

Binary file not shown.

View File

@ -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

View File

@ -27,6 +27,7 @@ require 'faker'
require 'tox'
require 'opus_file'
require 'vorbis_file'
require 'support/random_port'

View File

@ -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'