Skip to content

Commit 7c256db

Browse files
authored
Merge pull request #2753 from ethancrawford/include_all_onset_slices
Sample Onsets - include final sample slice in onset_slices
2 parents fe706be + 1bf8b3b commit 7c256db

File tree

3 files changed

+54
-4
lines changed

3 files changed

+54
-4
lines changed

app/server/ruby/lib/sonicpi/lang/sound.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3685,7 +3685,7 @@ def normalise_and_resolve_sample_args(path, args_h, info, combine_tls=false)
36853685
elsif onset_idx.is_a? Proc
36863686
onset = onset_idx.call(onsets)
36873687
onset = onset[0] if is_list_like?(onset)
3688-
raise "Result of onset: proc should be a Map such as {:start => 0, :finish => 0.125}. Got: #{res.inspect}" unless onset.respond_to?(:has_key?) && onset[:start].is_a?(Numeric) && onset[:finish].is_a?(Numeric)
3688+
raise "Result of onset: proc should be a Map such as {:start => 0, :finish => 0.125}. Got: #{onset.inspect}" unless onset.respond_to?(:has_key?) && onset[:start].is_a?(Numeric) && onset[:finish].is_a?(Numeric)
36893689
else
36903690
raise "Unknown sample onset: value. Expected a number or a proc. Got #{onset_idx.inspect}"
36913691
end

app/server/ruby/lib/sonicpi/samplebuffer.rb

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -131,12 +131,14 @@ def onsets(stretch=1)
131131

132132
def onset_slices
133133
return @aubio_slices if @aubio_slices
134-
ons = onsets
134+
ons_bounds = onsets
135135
@aubio_sem.synchronize do
136136
return @aubio_slices if @aubio_slices
137137
res = []
138-
ons[0...-1].each_with_index do |onset, idx|
139-
res << {:start => onset, :finish => ons[idx + 1], index: idx}
138+
ons_bounds << 0 if ons_bounds.empty?
139+
ons_bounds << 1 if ons_bounds[-1] != 1
140+
ons_bounds.each_cons(2).each_with_index do |(start, finish), idx|
141+
res << { start: start, finish: finish, index: idx }
140142
end
141143
@aubio_slices = res.ring
142144
end
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
#--
2+
# This file is part of Sonic Pi: http://sonic-pi.net
3+
# Full project source: https://github.com/samaaron/sonic-pi
4+
# License: https://github.com/samaaron/sonic-pi/blob/main/LICENSE.md
5+
#
6+
# Copyright 2013, 2014, 2015, 2016 by Sam Aaron (http://sam.aaron.name).
7+
# All rights reserved.
8+
#
9+
# Permission is granted for use, copying, modification, and
10+
# distribution of modified versions of this work as long as this
11+
# notice is included.
12+
#++
13+
14+
require_relative './setup_test'
15+
require_relative '../lib/sonicpi/samplebuffer'
16+
require 'mocha/setup'
17+
18+
module SonicPi
19+
class SampleBufferTester < Minitest::Test
20+
def setup
21+
@mock_buffer = SampleBuffer.new(nil, '/foo.wav')
22+
end
23+
24+
def test_onset_slices_with_no_onsets
25+
@mock_buffer.stubs(:onsets).returns([])
26+
expected_slices = [{ start: 0, finish: 1, index: 0 }].ring
27+
28+
assert_equal(@mock_buffer.onset_slices, expected_slices)
29+
end
30+
31+
def test_onset_slices_with_single_onset
32+
@mock_buffer.stubs(:onsets).returns([0])
33+
expected_slices = [{ start: 0, finish: 1, index: 0 }].ring
34+
35+
assert_equal(@mock_buffer.onset_slices, expected_slices)
36+
end
37+
38+
def test_onset_slices_with_multiple_onsets
39+
@mock_buffer.stubs(:onsets).returns([0, 0.5])
40+
expected_slices = [
41+
{ start: 0, finish: 0.5, index: 0 },
42+
{ start: 0.5, finish: 1, index: 1 }
43+
].ring
44+
45+
assert_equal(@mock_buffer.onset_slices, expected_slices)
46+
end
47+
end
48+
end

0 commit comments

Comments
 (0)