aubio 0.3.0 → 0.3.5
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +5 -5
- data/.gitignore +1 -0
- data/Gemfile +2 -0
- data/README.md +13 -2
- data/Rakefile +7 -5
- data/aubio-ffi-generator.rb +8 -6
- data/aubio.gemspec +19 -17
- data/bin/console +4 -3
- data/lib/aubio/aubio-ffi.rb +51 -49
- data/lib/aubio/beats.rb +55 -52
- data/lib/aubio/legacy_api.rb +40 -39
- data/lib/aubio/onsets.rb +52 -52
- data/lib/aubio/pitches.rb +62 -54
- data/lib/aubio/version.rb +3 -1
- data/lib/ruby_aubio.rb +132 -0
- metadata +27 -15
- data/aubio.rb +0 -467
- data/lib/aubio.rb +0 -93
data/lib/aubio/version.rb
CHANGED
data/lib/ruby_aubio.rb
ADDED
@@ -0,0 +1,132 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
require_relative 'aubio/version'
|
4
|
+
require_relative 'aubio/aubio-ffi'
|
5
|
+
require_relative 'aubio/onsets'
|
6
|
+
require_relative 'aubio/pitches'
|
7
|
+
require_relative 'aubio/beats'
|
8
|
+
|
9
|
+
module Aubio
|
10
|
+
class AubioException < RuntimeError; end
|
11
|
+
class FileNotFound < AubioException; end
|
12
|
+
class AlreadyClosed < AubioException; end
|
13
|
+
class InvalidAudioInput < AubioException; end
|
14
|
+
|
15
|
+
class Base
|
16
|
+
def initialize(path, params)
|
17
|
+
raise FileNotFound unless File.file?(path)
|
18
|
+
|
19
|
+
sample_rate = params[:sample_rate] || 44_100
|
20
|
+
hop_size = params[:hop_size] || 512
|
21
|
+
|
22
|
+
@is_closed = false
|
23
|
+
@source = Api.new_aubio_source(path, sample_rate, hop_size)
|
24
|
+
@params = params
|
25
|
+
|
26
|
+
check_for_valid_audio_source(path)
|
27
|
+
end
|
28
|
+
|
29
|
+
def close
|
30
|
+
Api.del_aubio_source(@source)
|
31
|
+
@is_closed = true
|
32
|
+
end
|
33
|
+
|
34
|
+
def onsets
|
35
|
+
check_for_closed
|
36
|
+
|
37
|
+
Onsets.new(@source, @params).each
|
38
|
+
end
|
39
|
+
|
40
|
+
def pitches
|
41
|
+
check_for_closed
|
42
|
+
|
43
|
+
Pitches.new(@source, @params).each
|
44
|
+
end
|
45
|
+
|
46
|
+
def enum_beats
|
47
|
+
check_for_closed
|
48
|
+
|
49
|
+
Beats.new(@source, @params).each
|
50
|
+
end
|
51
|
+
|
52
|
+
def beats
|
53
|
+
check_for_closed
|
54
|
+
|
55
|
+
beats = Beats.new(@source, @params).each.to_a
|
56
|
+
|
57
|
+
# fill in the zero beat
|
58
|
+
beats = beats.unshift(
|
59
|
+
beats.first.merge({
|
60
|
+
confidence: 1,
|
61
|
+
s: 0.0,
|
62
|
+
ms: 0.0,
|
63
|
+
sample_no: 0,
|
64
|
+
rel_start: 0.0
|
65
|
+
})
|
66
|
+
)
|
67
|
+
|
68
|
+
# fetch the rel_end from the next beat
|
69
|
+
# using 1.0 for the last beat
|
70
|
+
beats = beats.each_cons(2).map do |a, b|
|
71
|
+
a.merge({
|
72
|
+
rel_end: (b[:rel_start] || 1.0)
|
73
|
+
})
|
74
|
+
end
|
75
|
+
|
76
|
+
# set minimum inter-onset interval in seconds
|
77
|
+
# allows for 4/4 at 400bpm (faster than most music)
|
78
|
+
# filters beats detected too closely together
|
79
|
+
minioi = @params[:minioi] || 0.15
|
80
|
+
filtered_beats = [beats.first]
|
81
|
+
beats.each do |b|
|
82
|
+
filtered_beats << b if (b[:s] - filtered_beats.last[:s]) > minioi
|
83
|
+
end
|
84
|
+
|
85
|
+
# TODO: are there other smoothing methods that would be useful here?
|
86
|
+
filtered_beats
|
87
|
+
end
|
88
|
+
|
89
|
+
def bpm
|
90
|
+
check_for_closed
|
91
|
+
|
92
|
+
beat_locations = beats
|
93
|
+
beat_periods = beat_locations.each_cons(2).map { |a, b| b[:s] - a[:s] }
|
94
|
+
|
95
|
+
return 60.0 if beat_locations.length == 1
|
96
|
+
|
97
|
+
# use interquartile median to discourage outliers
|
98
|
+
s = beat_periods.length
|
99
|
+
qrt_lower_idx = (s / 4.0).floor
|
100
|
+
qrt_upper_idx = qrt_lower_idx * 3
|
101
|
+
interquartile_beat_periods = beat_periods[qrt_lower_idx..qrt_upper_idx]
|
102
|
+
|
103
|
+
# Calculate median
|
104
|
+
iqs = interquartile_beat_periods.length
|
105
|
+
|
106
|
+
iq_median_beat_period = interquartile_beat_periods.sort[(iqs / 2.0).floor - 1]
|
107
|
+
60.0 / iq_median_beat_period
|
108
|
+
end
|
109
|
+
|
110
|
+
private
|
111
|
+
|
112
|
+
def check_for_closed
|
113
|
+
raise AlreadyClosed if @is_closed
|
114
|
+
end
|
115
|
+
|
116
|
+
def check_for_valid_audio_source(path)
|
117
|
+
@source.read_pointer
|
118
|
+
rescue FFI::NullPointerError
|
119
|
+
raise InvalidAudioInput, %(
|
120
|
+
|
121
|
+
Couldn't read file at #{path}
|
122
|
+
Did you install aubio with libsndfile support?
|
123
|
+
)
|
124
|
+
end
|
125
|
+
end
|
126
|
+
end
|
127
|
+
|
128
|
+
module Aubio
|
129
|
+
def self.open(path, params = {})
|
130
|
+
Base.new(path, params)
|
131
|
+
end
|
132
|
+
end
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: aubio
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.3.
|
4
|
+
version: 0.3.5
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Xavier Riley
|
8
8
|
autorequire:
|
9
9
|
bindir: exe
|
10
10
|
cert_chain: []
|
11
|
-
date:
|
11
|
+
date: 2020-08-26 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: ffi
|
@@ -28,30 +28,44 @@ dependencies:
|
|
28
28
|
name: bundler
|
29
29
|
requirement: !ruby/object:Gem::Requirement
|
30
30
|
requirements:
|
31
|
-
- - "
|
31
|
+
- - ">="
|
32
32
|
- !ruby/object:Gem::Version
|
33
|
-
version: '
|
33
|
+
version: '0'
|
34
34
|
type: :development
|
35
35
|
prerelease: false
|
36
36
|
version_requirements: !ruby/object:Gem::Requirement
|
37
37
|
requirements:
|
38
|
-
- - "
|
38
|
+
- - ">="
|
39
39
|
- !ruby/object:Gem::Version
|
40
|
-
version: '
|
40
|
+
version: '0'
|
41
41
|
- !ruby/object:Gem::Dependency
|
42
42
|
name: rake
|
43
43
|
requirement: !ruby/object:Gem::Requirement
|
44
44
|
requirements:
|
45
|
-
- - "
|
45
|
+
- - ">="
|
46
46
|
- !ruby/object:Gem::Version
|
47
|
-
version:
|
47
|
+
version: 12.3.3
|
48
|
+
type: :runtime
|
49
|
+
prerelease: false
|
50
|
+
version_requirements: !ruby/object:Gem::Requirement
|
51
|
+
requirements:
|
52
|
+
- - ">="
|
53
|
+
- !ruby/object:Gem::Version
|
54
|
+
version: 12.3.3
|
55
|
+
- !ruby/object:Gem::Dependency
|
56
|
+
name: ffi_gen
|
57
|
+
requirement: !ruby/object:Gem::Requirement
|
58
|
+
requirements:
|
59
|
+
- - ">="
|
60
|
+
- !ruby/object:Gem::Version
|
61
|
+
version: '0'
|
48
62
|
type: :development
|
49
63
|
prerelease: false
|
50
64
|
version_requirements: !ruby/object:Gem::Requirement
|
51
65
|
requirements:
|
52
|
-
- - "
|
66
|
+
- - ">="
|
53
67
|
- !ruby/object:Gem::Version
|
54
|
-
version: '
|
68
|
+
version: '0'
|
55
69
|
- !ruby/object:Gem::Dependency
|
56
70
|
name: minitest
|
57
71
|
requirement: !ruby/object:Gem::Requirement
|
@@ -81,7 +95,7 @@ dependencies:
|
|
81
95
|
- !ruby/object:Gem::Version
|
82
96
|
version: '0'
|
83
97
|
- !ruby/object:Gem::Dependency
|
84
|
-
name:
|
98
|
+
name: rubocop
|
85
99
|
requirement: !ruby/object:Gem::Requirement
|
86
100
|
requirements:
|
87
101
|
- - ">="
|
@@ -113,16 +127,15 @@ files:
|
|
113
127
|
- Rakefile
|
114
128
|
- aubio-ffi-generator.rb
|
115
129
|
- aubio.gemspec
|
116
|
-
- aubio.rb
|
117
130
|
- bin/console
|
118
131
|
- bin/setup
|
119
|
-
- lib/aubio.rb
|
120
132
|
- lib/aubio/aubio-ffi.rb
|
121
133
|
- lib/aubio/beats.rb
|
122
134
|
- lib/aubio/legacy_api.rb
|
123
135
|
- lib/aubio/onsets.rb
|
124
136
|
- lib/aubio/pitches.rb
|
125
137
|
- lib/aubio/version.rb
|
138
|
+
- lib/ruby_aubio.rb
|
126
139
|
homepage: https://github.com/xavriley/ruby-aubio
|
127
140
|
licenses:
|
128
141
|
- MIT
|
@@ -142,8 +155,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
142
155
|
- !ruby/object:Gem::Version
|
143
156
|
version: '0'
|
144
157
|
requirements: []
|
145
|
-
|
146
|
-
rubygems_version: 2.6.7
|
158
|
+
rubygems_version: 3.1.2
|
147
159
|
signing_key:
|
148
160
|
specification_version: 4
|
149
161
|
summary: Ruby bindings for the aubio audio library
|
data/aubio.rb
DELETED
@@ -1,467 +0,0 @@
|
|
1
|
-
require 'ffi'
|
2
|
-
|
3
|
-
module Aubio
|
4
|
-
extend FFI::Library
|
5
|
-
ffi_lib '/usr/local/Cellar/aubio/0.4.2/lib/libaubio.4.2.2.dylib'
|
6
|
-
|
7
|
-
# tempo
|
8
|
-
attach_function :new_aubio_tempo, [ :string, :int, :int, :int ], :pointer
|
9
|
-
attach_function :aubio_tempo_do, [:pointer, :pointer, :pointer], :void
|
10
|
-
attach_function :aubio_tempo_get_last, [:pointer], :int
|
11
|
-
attach_function :aubio_tempo_get_last_s, [:pointer], :float
|
12
|
-
attach_function :aubio_tempo_get_last_ms, [:pointer], :float
|
13
|
-
attach_function :aubio_tempo_set_silence, [:pointer, :float], :int
|
14
|
-
attach_function :aubio_tempo_get_silence, [:pointer], :float
|
15
|
-
attach_function :aubio_tempo_set_threshold, [:pointer, :float], :int
|
16
|
-
attach_function :aubio_tempo_get_threshold, [:pointer], :float
|
17
|
-
attach_function :aubio_tempo_get_bpm, [:pointer], :float
|
18
|
-
attach_function :aubio_tempo_get_confidence, [:pointer], :float
|
19
|
-
attach_function :del_aubio_tempo, [:pointer], :void
|
20
|
-
|
21
|
-
# beattracking / misc
|
22
|
-
attach_function :new_aubio_beattracking, [:int, :int, :int], :pointer
|
23
|
-
attach_function :aubio_beattracking_do, [:pointer, :pointer, :pointer], :void
|
24
|
-
attach_function :aubio_beattracking_get_bpm, [:pointer], :float
|
25
|
-
attach_function :aubio_filter_do, [:pointer, :pointer], :void
|
26
|
-
attach_function :new_aubio_filter_a_weighting, [:int], :pointer
|
27
|
-
|
28
|
-
# source
|
29
|
-
attach_function :new_aubio_source, [:string, :int, :int], :pointer
|
30
|
-
attach_function :aubio_source_do, [:pointer, :pointer, :pointer], :void
|
31
|
-
attach_function :aubio_source_do_multi, [:pointer, :pointer, :pointer], :void
|
32
|
-
attach_function :aubio_source_get_samplerate, [:pointer], :int
|
33
|
-
attach_function :aubio_source_get_channels, [:pointer], :int
|
34
|
-
attach_function :aubio_source_seek, [:pointer, :int], :int
|
35
|
-
attach_function :aubio_source_close, [:pointer], :int
|
36
|
-
attach_function :del_aubio_source, [:pointer], :void
|
37
|
-
|
38
|
-
# sink
|
39
|
-
attach_function :new_aubio_sink, [:string, :int], :pointer
|
40
|
-
attach_function :aubio_sink_preset_samplerate, [:pointer, :int], :void
|
41
|
-
attach_function :aubio_sink_preset_channels, [:pointer, :int], :void
|
42
|
-
attach_function :aubio_sink_get_samplerate, [:pointer], :int
|
43
|
-
attach_function :aubio_sink_get_channels, [:pointer], :int
|
44
|
-
attach_function :aubio_sink_do, [:pointer, :pointer, :int], :void
|
45
|
-
attach_function :aubio_sink_do_multi, [:pointer, :pointer, :int], :void
|
46
|
-
attach_function :aubio_sink_close, [:pointer], :int
|
47
|
-
attach_function :del_aubio_sink, [:pointer], :void
|
48
|
-
|
49
|
-
# onset
|
50
|
-
attach_function :new_aubio_onset, [:string, :int, :int, :int], :pointer
|
51
|
-
attach_function :aubio_onset_do, [:pointer, :pointer, :pointer], :void
|
52
|
-
attach_function :aubio_onset_get_last, [:pointer], :int
|
53
|
-
attach_function :aubio_onset_get_last_s, [:pointer], :float
|
54
|
-
attach_function :aubio_onset_get_last_ms, [:pointer], :float
|
55
|
-
attach_function :aubio_onset_set_silence, [:pointer, :float], :int
|
56
|
-
attach_function :aubio_onset_get_silence, [:pointer], :float
|
57
|
-
attach_function :aubio_onset_get_descriptor, [:pointer], :float
|
58
|
-
attach_function :aubio_onset_get_thresholded_descriptor, [:pointer], :float
|
59
|
-
attach_function :aubio_onset_set_threshold, [:pointer, :float], :int
|
60
|
-
attach_function :aubio_onset_set_minioi, [:pointer, :int], :int
|
61
|
-
attach_function :aubio_onset_set_minioi_s, [:pointer, :int], :int
|
62
|
-
attach_function :aubio_onset_set_minioi_ms, [:pointer, :float], :int
|
63
|
-
attach_function :aubio_onset_set_delay, [:pointer, :int], :int
|
64
|
-
attach_function :aubio_onset_set_delay_s, [:pointer, :int], :int
|
65
|
-
attach_function :aubio_onset_set_delay_ms, [:pointer, :float], :int
|
66
|
-
attach_function :aubio_onset_get_minioi, [:pointer], :int
|
67
|
-
attach_function :aubio_onset_get_minioi_s, [:pointer], :float
|
68
|
-
attach_function :aubio_onset_get_minioi_ms, [:pointer], :float
|
69
|
-
attach_function :aubio_onset_get_delay, [:pointer], :int
|
70
|
-
attach_function :aubio_onset_get_delay_s, [:pointer], :float
|
71
|
-
attach_function :aubio_onset_get_delay_ms, [:pointer], :float
|
72
|
-
attach_function :aubio_onset_get_threshold, [:pointer], :float
|
73
|
-
attach_function :del_aubio_onset, [:pointer], :void
|
74
|
-
|
75
|
-
# pitch
|
76
|
-
attach_function :new_aubio_pitch, [:string, :int, :int, :int], :pointer
|
77
|
-
attach_function :aubio_pitch_do, [:pointer, :pointer, :pointer], :void
|
78
|
-
attach_function :aubio_pitch_set_tolerance, [:pointer, :int], :int
|
79
|
-
attach_function :aubio_pitch_set_unit, [:pointer, :string], :int
|
80
|
-
attach_function :aubio_pitch_set_silence, [:pointer, :float], :int
|
81
|
-
attach_function :aubio_pitch_get_silence, [:pointer], :float
|
82
|
-
attach_function :aubio_pitch_get_confidence, [:pointer], :float
|
83
|
-
attach_function :del_aubio_pitch, [:pointer], :void
|
84
|
-
|
85
|
-
# new fvec
|
86
|
-
attach_function :new_fvec, [:int], :pointer
|
87
|
-
attach_function :del_fvec, [:pointer], :void
|
88
|
-
attach_function :fvec_get_sample, [:pointer, :int], :float
|
89
|
-
attach_function :fvec_set_sample, [:pointer, :float, :int], :void
|
90
|
-
attach_function :fvec_get_data, [:pointer], :float
|
91
|
-
attach_function :fvec_print, [:pointer], :void
|
92
|
-
attach_function :fvec_set_all, [:pointer, :float], :void
|
93
|
-
attach_function :fvec_zeros, [:pointer], :void
|
94
|
-
attach_function :fvec_rev, [:pointer], :void
|
95
|
-
attach_function :fvec_weight, [:pointer, :pointer], :void
|
96
|
-
attach_function :fvec_copy, [:pointer, :pointer], :void
|
97
|
-
attach_function :fvec_ones, [:pointer], :void
|
98
|
-
|
99
|
-
def self.onsets(path, params={})
|
100
|
-
sample_rate = params[:sample_rate] || 44100
|
101
|
-
window_size = params[:window_size] || 1024
|
102
|
-
hop_size = params[:hop_size] || 512
|
103
|
-
|
104
|
-
# parser.add_option("-O","--onset-method",
|
105
|
-
# action="store", dest="onset_method", default='default',
|
106
|
-
# metavar = "<onset_method>",
|
107
|
-
# help="onset detection method [default=default] \
|
108
|
-
# complexdomain|hfc|phase|specdiff|energy|kl|mkl")
|
109
|
-
onset_method = params[:onset_method] || "default"
|
110
|
-
|
111
|
-
# # cutting methods
|
112
|
-
# parser.add_option("-b","--beat",
|
113
|
-
# action="store_true", dest="beat", default=False,
|
114
|
-
# help="use beat locations")
|
115
|
-
beat = params[:beat] || false
|
116
|
-
# """
|
117
|
-
# parser.add_option("-S","--silencecut",
|
118
|
-
# action="store_true", dest="silencecut", default=False,
|
119
|
-
# help="use silence locations")
|
120
|
-
silencecut = params[:silencecut] || false
|
121
|
-
|
122
|
-
# parser.add_option("-s","--silence",
|
123
|
-
# metavar = "<value>",
|
124
|
-
# action="store", dest="silence", default=-70,
|
125
|
-
# help="silence threshold [default=-70]")
|
126
|
-
silence = params[:silence] || -70
|
127
|
-
|
128
|
-
# """
|
129
|
-
# # algorithm parameters
|
130
|
-
# parser.add_option("-r", "--samplerate",
|
131
|
-
# metavar = "<freq>", type='int',
|
132
|
-
# action="store", dest="samplerate", default=0,
|
133
|
-
# help="samplerate at which the file should be represented")
|
134
|
-
# parser.add_option("-B","--bufsize",
|
135
|
-
# action="store", dest="bufsize", default=512,
|
136
|
-
# metavar = "<size>", type='int',
|
137
|
-
# help="buffer size [default=512]")
|
138
|
-
# parser.add_option("-H","--hopsize",
|
139
|
-
# metavar = "<size>", type='int',
|
140
|
-
# action="store", dest="hopsize", default=256,
|
141
|
-
# help="overlap size [default=256]")
|
142
|
-
# parser.add_option("-t","--onset-threshold",
|
143
|
-
# metavar = "<value>", type="float",
|
144
|
-
# action="store", dest="threshold", default=0.3,
|
145
|
-
# help="onset peak picking threshold [default=0.3]")
|
146
|
-
# parser.add_option("-c","--cut",
|
147
|
-
# action="store_true", dest="cut", default=False,
|
148
|
-
# help="cut input sound file at detected labels \
|
149
|
-
# best used with option -L")
|
150
|
-
|
151
|
-
# # minioi
|
152
|
-
# parser.add_option("-M","--minioi",
|
153
|
-
# metavar = "<value>", type='string',
|
154
|
-
# action="store", dest="minioi", default="12ms",
|
155
|
-
# help="minimum inter onset interval [default=12ms]")
|
156
|
-
|
157
|
-
# """
|
158
|
-
# parser.add_option("-D","--delay",
|
159
|
-
# action = "store", dest = "delay", type = "float",
|
160
|
-
# metavar = "<seconds>", default=0,
|
161
|
-
# help="number of seconds to take back [default=system]\
|
162
|
-
# default system delay is 3*hopsize/samplerate")
|
163
|
-
# parser.add_option("-C","--dcthreshold",
|
164
|
-
# metavar = "<value>",
|
165
|
-
# action="store", dest="dcthreshold", default=1.,
|
166
|
-
# help="onset peak picking DC component [default=1.]")
|
167
|
-
# parser.add_option("-L","--localmin",
|
168
|
-
# action="store_true", dest="localmin", default=False,
|
169
|
-
# help="use local minima after peak detection")
|
170
|
-
# parser.add_option("-d","--derivate",
|
171
|
-
# action="store_true", dest="derivate", default=False,
|
172
|
-
# help="derivate onset detection function")
|
173
|
-
# parser.add_option("-z","--zerocross",
|
174
|
-
# metavar = "<value>",
|
175
|
-
# action="store", dest="zerothres", default=0.008,
|
176
|
-
# help="zero-crossing threshold for slicing [default=0.00008]")
|
177
|
-
# """
|
178
|
-
# # plotting functions
|
179
|
-
# """
|
180
|
-
# parser.add_option("-p","--plot",
|
181
|
-
# action="store_true", dest="plot", default=False,
|
182
|
-
# help="draw plot")
|
183
|
-
# parser.add_option("-x","--xsize",
|
184
|
-
# metavar = "<size>",
|
185
|
-
# action="store", dest="xsize", default=1.,
|
186
|
-
# type='float', help="define xsize for plot")
|
187
|
-
# parser.add_option("-y","--ysize",
|
188
|
-
# metavar = "<size>",
|
189
|
-
# action="store", dest="ysize", default=1.,
|
190
|
-
# type='float', help="define ysize for plot")
|
191
|
-
# parser.add_option("-f","--function",
|
192
|
-
# action="store_true", dest="func", default=False,
|
193
|
-
# help="print detection function")
|
194
|
-
# parser.add_option("-n","--no-onsets",
|
195
|
-
# action="store_true", dest="nplot", default=False,
|
196
|
-
# help="do not plot detected onsets")
|
197
|
-
# parser.add_option("-O","--outplot",
|
198
|
-
# metavar = "<output_image>",
|
199
|
-
# action="store", dest="outplot", default=None,
|
200
|
-
# help="save plot to output.{ps,png}")
|
201
|
-
# parser.add_option("-F","--spectrogram",
|
202
|
-
# action="store_true", dest="spectro", default=False,
|
203
|
-
# help="add spectrogram to the plot")
|
204
|
-
# """
|
205
|
-
# parser.add_option("-o","--output", type = str,
|
206
|
-
# metavar = "<outputdir>",
|
207
|
-
# action="store", dest="output_directory", default=None,
|
208
|
-
# help="specify path where slices of the original file should be created")
|
209
|
-
# parser.add_option("--cut-until-nsamples", type = int,
|
210
|
-
# metavar = "<samples>",
|
211
|
-
# action = "store", dest = "cut_until_nsamples", default = None,
|
212
|
-
# help="how many extra samples should be added at the end of each slice")
|
213
|
-
# parser.add_option("--cut-until-nslices", type = int,
|
214
|
-
# metavar = "<slices>",
|
215
|
-
# action = "store", dest = "cut_until_nslices", default = None,
|
216
|
-
# help="how many extra slices should be added at the end of each slice")
|
217
|
-
|
218
|
-
# parser.add_option("-v","--verbose",
|
219
|
-
# action="store_true", dest="verbose", default=True,
|
220
|
-
# help="make lots of noise [default]")
|
221
|
-
# parser.add_option("-q","--quiet",
|
222
|
-
# action="store_false", dest="verbose", default=True,
|
223
|
-
# help="be quiet")
|
224
|
-
# (options, args) = parser.parse_args()
|
225
|
-
# if not options.source_file:
|
226
|
-
# import os.path
|
227
|
-
# if len(args) == 1:
|
228
|
-
# options.source_file = args[0]
|
229
|
-
# else:
|
230
|
-
# print "no file name given\n", usage
|
231
|
-
# sys.exit(1)
|
232
|
-
# return options, args
|
233
|
-
|
234
|
-
source = new_aubio_source(path, sample_rate, hop_size)
|
235
|
-
onset = new_aubio_onset("default", 512, hop_size)
|
236
|
-
aubio_onset_set_minioi_ms(onset, 12.0)
|
237
|
-
aubio_onset_set_threshold(onset, 0.3)
|
238
|
-
|
239
|
-
timestamps = []
|
240
|
-
total_frames = 0
|
241
|
-
# create output for source
|
242
|
-
samples = new_fvec(hop_size)
|
243
|
-
total_frames_counter = 0
|
244
|
-
tmp_read = FFI::MemoryPointer.new(:int)
|
245
|
-
|
246
|
-
loop do
|
247
|
-
aubio_source_do(source, samples, tmp_read)
|
248
|
-
|
249
|
-
end
|
250
|
-
# if options.beat:
|
251
|
-
# o = tempo(options.onset_method, bufsize, hopsize)
|
252
|
-
# else:
|
253
|
-
# o = onset(options.onset_method, bufsize, hopsize)
|
254
|
-
# if options.minioi:
|
255
|
-
# if options.minioi.endswith('ms'):
|
256
|
-
# o.set_minioi_ms(int(options.minioi[:-2]))
|
257
|
-
# elif options.minioi.endswith('s'):
|
258
|
-
# o.set_minioi_s(int(options.minioi[:-1]))
|
259
|
-
# else:
|
260
|
-
# o.set_minioi(int(options.minioi))
|
261
|
-
# o.set_threshold(options.threshold)
|
262
|
-
|
263
|
-
# timestamps = []
|
264
|
-
# total_frames = 0
|
265
|
-
# # analyze pass
|
266
|
-
# while True:
|
267
|
-
# samples, read = s()
|
268
|
-
# if o(samples):
|
269
|
-
# timestamps.append (o.get_last())
|
270
|
-
# if options.verbose: print "%.4f" % o.get_last_s()
|
271
|
-
# total_frames += read
|
272
|
-
# if read < hopsize: break
|
273
|
-
# del s
|
274
|
-
# # print some info
|
275
|
-
# nstamps = len(timestamps)
|
276
|
-
# duration = float (total_frames) / float(samplerate)
|
277
|
-
# info = 'found %(nstamps)d timestamps in %(source_file)s' % locals()
|
278
|
-
# info += ' (total %(duration).2fs at %(samplerate)dHz)\n' % locals()
|
279
|
-
# sys.stderr.write(info)
|
280
|
-
|
281
|
-
# # cutting pass
|
282
|
-
# if options.cut and nstamps > 0:
|
283
|
-
# # generate output files
|
284
|
-
# from aubio.slicing import slice_source_at_stamps
|
285
|
-
# timestamps_end = None
|
286
|
-
# if options.cut_until_nslices and options.cut_until_nsamples:
|
287
|
-
# print "warning: using cut_until_nslices, but cut_until_nsamples is set"
|
288
|
-
# if options.cut_until_nsamples:
|
289
|
-
# timestamps_end = [t + options.cut_until_nsamples for t in timestamps[1:]]
|
290
|
-
# timestamps_end += [ 1e120 ]
|
291
|
-
# if options.cut_until_nslices:
|
292
|
-
# timestamps_end = [t for t in timestamps[1 + options.cut_until_nslices:]]
|
293
|
-
# timestamps_end += [ 1e120 ] * (options.cut_until_nslices + 1)
|
294
|
-
# slice_source_at_stamps(source_file, timestamps, timestamps_end = timestamps_end,
|
295
|
-
# output_dir = options.output_directory,
|
296
|
-
# samplerate = samplerate)
|
297
|
-
|
298
|
-
# # print some info
|
299
|
-
# duration = float (total_frames) / float(samplerate)
|
300
|
-
# info = 'created %(nstamps)d slices from %(source_file)s' % locals()
|
301
|
-
# info += ' (total %(duration).2fs at %(samplerate)dHz)\n' % locals()
|
302
|
-
# sys.stderr.write(info)
|
303
|
-
end
|
304
|
-
|
305
|
-
def self.get_features(path, params={})
|
306
|
-
sample_rate = params[:sample_rate] || 44100
|
307
|
-
window_size = params[:window_size] || 1024
|
308
|
-
hop_size = params[:hop_size] || 512
|
309
|
-
|
310
|
-
source = new_aubio_source(path, sample_rate, hop_size)
|
311
|
-
calculated_sample_rate = aubio_source_get_samplerate(source)
|
312
|
-
puts "samplerate: #{calculated_sample_rate}"
|
313
|
-
|
314
|
-
onset = new_aubio_onset('default', window_size, hop_size, sample_rate)
|
315
|
-
aubio_onset_set_minioi_ms(onset, 12.0)
|
316
|
-
aubio_onset_set_threshold(onset, 0.3)
|
317
|
-
onsets = []
|
318
|
-
|
319
|
-
pitch = new_aubio_pitch('default', window_size, hop_size, sample_rate)
|
320
|
-
aubio_pitch_set_unit(pitch, 'Hz')
|
321
|
-
|
322
|
-
# create output for source
|
323
|
-
samples = new_fvec(hop_size)
|
324
|
-
# create output for pitch and beat
|
325
|
-
out_fvec = new_fvec(1)
|
326
|
-
total_frames_counter = 0
|
327
|
-
tmp_read = FFI::MemoryPointer.new(:int)
|
328
|
-
|
329
|
-
loop do
|
330
|
-
aubio_source_do(source, samples, tmp_read)
|
331
|
-
|
332
|
-
aubio_pitch_do(pitch, samples, out_fvec)
|
333
|
-
|
334
|
-
cur_time = total_frames_counter / sample_rate
|
335
|
-
last_pitch = fvec_get_sample(out_fvec, 0);
|
336
|
-
|
337
|
-
#puts "pitch at #{cur_time} seconds: #{last_pitch} Hz"
|
338
|
-
|
339
|
-
aubio_onset_do(onset, samples, out_fvec)
|
340
|
-
is_onset = fvec_get_sample(out_fvec, 0)
|
341
|
-
|
342
|
-
if is_onset > 0.0
|
343
|
-
last_onset = aubio_onset_get_last_s(onset)
|
344
|
-
onsets << last_onset
|
345
|
-
puts "onset at #{last_onset}"
|
346
|
-
end
|
347
|
-
|
348
|
-
read = tmp_read.read_int
|
349
|
-
total_frames_counter += read
|
350
|
-
if(read != hop_size) then
|
351
|
-
break
|
352
|
-
end
|
353
|
-
end
|
354
|
-
|
355
|
-
cur_time = total_frames_counter.to_f / sample_rate;
|
356
|
-
puts "total time : #{cur_time} seconds (#{total_frames_counter} frames)"
|
357
|
-
puts "found #{onsets.length} onsets total"
|
358
|
-
|
359
|
-
# cleanup
|
360
|
-
del_aubio_source(source);
|
361
|
-
del_aubio_onset(onset);
|
362
|
-
del_aubio_pitch(pitch);
|
363
|
-
|
364
|
-
onsets
|
365
|
-
end
|
366
|
-
|
367
|
-
# # change comments, swap args, convert to sym
|
368
|
-
|
369
|
-
# intPtr = 'int'
|
370
|
-
# stringPtr = "string" #ref.refType(ref.types.CString);
|
371
|
-
|
372
|
-
# {
|
373
|
-
# "aubio_tempo_do": [ "void", [ "pointer", "pointer", "pointer"]],
|
374
|
-
# "aubio_tempo_get_last": [ "int", ["pointer"]],
|
375
|
-
# "aubio_tempo_get_last_s": [ "float", ["pointer"]],
|
376
|
-
# "aubio_tempo_get_last_ms": [ "float", ["pointer"]],
|
377
|
-
# "aubio_tempo_set_silence": [ "int", ["pointer", "float"]],
|
378
|
-
# "aubio_tempo_get_silence": [ "float", ["pointer"]],
|
379
|
-
# "aubio_tempo_set_threshold": [ "int", ["pointer", "float"]],
|
380
|
-
# "aubio_tempo_get_threshold": [ "float", ["pointer"]],
|
381
|
-
# "aubio_tempo_get_bpm": [ "float", ["pointer"]],
|
382
|
-
# "aubio_tempo_get_confidence": [ "float", ["pointer"]],
|
383
|
-
# "del_aubio_tempo": [ "void", ["pointer"]],
|
384
|
-
|
385
|
-
# # beattracking / misc
|
386
|
-
# "new_aubio_beattracking": [ "pointer", [ "int", "int", "int"]],
|
387
|
-
# "aubio_beattracking_do": [ "void", [ "pointer", "pointer", "pointer"]],
|
388
|
-
# "aubio_beattracking_get_bpm": [ "float", ["pointer"]],
|
389
|
-
# "aubio_filter_do": [ "void", [ "pointer", "pointer" ]],
|
390
|
-
# "new_aubio_filter_a_weighting": [ "pointer", [ "int" ]],
|
391
|
-
|
392
|
-
# # source
|
393
|
-
# "new_aubio_source": [ "pointer", [ "string", "int", "int" ]],
|
394
|
-
# "aubio_source_do": [ "void", [ "pointer", "pointer", intPtr ]],
|
395
|
-
# "aubio_source_do_multi": [ "void", [ "pointer", "pointer", intPtr ]],
|
396
|
-
# "aubio_source_get_samplerate": [ "int", [ "pointer" ]],
|
397
|
-
# "aubio_source_get_channels": [ "int", [ "pointer" ]],
|
398
|
-
# "aubio_source_seek": [ "int", [ "pointer", "int" ]],
|
399
|
-
# "aubio_source_close": [ "int", [ "pointer" ]],
|
400
|
-
# "del_aubio_source": [ "void", [ "pointer" ]],
|
401
|
-
|
402
|
-
# # sink
|
403
|
-
# "new_aubio_sink": [ "pointer", [ "string", "int" ]],
|
404
|
-
# "aubio_sink_preset_samplerate": [ "void", [ "pointer", "int" ]],
|
405
|
-
# "aubio_sink_preset_channels": [ "void", [ "pointer", "int" ]],
|
406
|
-
# "aubio_sink_get_samplerate": [ "int", [ "pointer" ]],
|
407
|
-
# "aubio_sink_get_channels": [ "int", [ "pointer" ]],
|
408
|
-
# "aubio_sink_do": ["void", ["pointer", "pointer", "int"]],
|
409
|
-
# "aubio_sink_do_multi": ["void", ["pointer", "pointer", "int"]],
|
410
|
-
# "aubio_sink_close": [ "int", [ "pointer" ]],
|
411
|
-
# "del_aubio_sink": [ "void", [ "pointer" ]],
|
412
|
-
|
413
|
-
# # onset
|
414
|
-
# "new_aubio_onset": [ "pointer", [ "string", "int", "int", "int"]],
|
415
|
-
# "aubio_onset_do": [ "void", [ "pointer", "pointer", "pointer"]],
|
416
|
-
# "aubio_onset_get_last": [ "int", ["pointer"]],
|
417
|
-
# "aubio_onset_get_last_s": [ "float", ["pointer"]],
|
418
|
-
# "aubio_onset_get_last_ms": [ "float", ["pointer"]],
|
419
|
-
# "aubio_onset_set_silence": [ "int", ["pointer", "float"]],
|
420
|
-
# "aubio_onset_get_silence": [ "float", ["pointer"]],
|
421
|
-
# "aubio_onset_get_descriptor": [ "float", ["pointer"]],
|
422
|
-
# "aubio_onset_get_thresholded_descriptor": [ "float", ["pointer"]],
|
423
|
-
# "aubio_onset_set_threshold": [ "int", ["pointer", "float"]],
|
424
|
-
# "aubio_onset_set_minioi": [ "int", ["pointer", "int"]],
|
425
|
-
# "aubio_onset_set_minioi_s": [ "int", ["pointer", "int"]],
|
426
|
-
# "aubio_onset_set_minioi_ms": [ "int", ["pointer", "float"]],
|
427
|
-
# "aubio_onset_set_delay": [ "int", ["pointer", "int"]],
|
428
|
-
# "aubio_onset_set_delay_s": [ "int", ["pointer", "int"]],
|
429
|
-
# "aubio_onset_set_delay_ms": [ "int", ["pointer", "float"]],
|
430
|
-
# "aubio_onset_get_minioi": [ "int", ["pointer"]],
|
431
|
-
# "aubio_onset_get_minioi_s": [ "float", ["pointer"]],
|
432
|
-
# "aubio_onset_get_minioi_ms": [ "float", ["pointer"]],
|
433
|
-
# "aubio_onset_get_delay": [ "int", ["pointer"]],
|
434
|
-
# "aubio_onset_get_delay_s": [ "float", ["pointer"]],
|
435
|
-
# "aubio_onset_get_delay_ms": [ "float", ["pointer"]],
|
436
|
-
# "aubio_onset_get_threshold": [ "float", ["pointer"]],
|
437
|
-
# "del_aubio_onset": [ "void", ["pointer"]],
|
438
|
-
|
439
|
-
# # pitch
|
440
|
-
# "new_aubio_pitch": [ "pointer", [ "string", "int", "int", "int"]],
|
441
|
-
# "aubio_pitch_do": ["void", ["pointer", "pointer", "pointer"]],
|
442
|
-
# "aubio_pitch_set_tolerance": [ "int", ["pointer", "int"]],
|
443
|
-
# "aubio_pitch_set_unit": ["int", ["pointer", "string"]],
|
444
|
-
# "aubio_pitch_set_silence": ["int", ["pointer", "float"]],
|
445
|
-
# "aubio_pitch_get_silence": ["float", ["pointer"]],
|
446
|
-
# "aubio_pitch_get_confidence": ["float", ["pointer"]],
|
447
|
-
# "del_aubio_pitch": [ "void", ["pointer"]],
|
448
|
-
|
449
|
-
# # fvec
|
450
|
-
# "new_fvec": [ "pointer", [ "int" ]],
|
451
|
-
# "del_fvec": [ "void", [ "pointer" ]],
|
452
|
-
# "fvec_get_sample": [ "float", [ "pointer", "int" ]],
|
453
|
-
# "fvec_set_sample": [ "void", [ "pointer", "float", "int" ]],
|
454
|
-
# "fvec_get_data": [ "float", [ "pointer" ]],
|
455
|
-
# "fvec_print": [ "void", [ "pointer" ]],
|
456
|
-
# "fvec_set_all": [ "void", [ "pointer", "float" ]],
|
457
|
-
# "fvec_zeros": [ "void", [ "pointer" ]],
|
458
|
-
# "fvec_rev": [ "void", [ "pointer" ]],
|
459
|
-
# "fvec_weight": [ "void", [ "pointer", "pointer" ]],
|
460
|
-
# "fvec_copy": [ "void", [ "pointer", "pointer" ]],
|
461
|
-
# "fvec_ones": [ "void", [ "pointer" ]],
|
462
|
-
# }.each do |k,v|
|
463
|
-
# puts "attach_function :#{k.to_sym}, #{v.last.map(&:to_sym)}, :#{v.first.to_sym}"
|
464
|
-
# end
|
465
|
-
end
|
466
|
-
|
467
|
-
puts Aubio.get_features("/Applications/Sonic Pi.app/etc/samples/loop_amen.flac", hop_size: 256, sample_rate: 44100).inspect
|