@@ -298,7 +298,7 @@ def get_event(*args)
298
298
# If we've time_warped into the future raise a timing exception
299
299
if __system_thread_locals . get ( :sonic_pi_spider_in_time_warp )
300
300
301
- if __system_thread_locals . get ( :sonic_pi_spider_time_warp_start ) < __system_thread_locals . get ( :sonic_pi_spider_time )
301
+ if __system_thread_locals . get ( :sonic_pi_spider_time_warp_start ) < __get_spider_time
302
302
raise TimingError , "Sadly, you may not time_warp into the future to call get, then bring the result back in time to now."
303
303
end
304
304
end
@@ -939,7 +939,6 @@ def clear
939
939
def time_warp ( times = 0 , params = nil , &block )
940
940
__schedule_delayed_blocks_and_messages!
941
941
942
-
943
942
raise ArgumentError , "time_warp requires a do/end block" unless block
944
943
prev_ctl_deltas = __system_thread_locals . get ( :sonic_pi_local_control_deltas )
945
944
prev_cache = __system_thread_locals . get ( :sonic_pi_spider_time_state_cache , [ ] )
@@ -953,45 +952,37 @@ def time_warp(times=0, params=nil, &block)
953
952
raise ArgumentError , "params needs to be a list-like thing" unless params . respond_to? :[]
954
953
raise ArgumentError , "times needs to be a list-like thing" unless times . respond_to? :each_with_index
955
954
956
- vt_orig = __get_spider_time
957
- density = __thread_locals . get ( :sonic_pi_local_spider_density ) || 1.0
958
- orig_sleep_mul_w_density = __get_spider_sleep_mul * density
959
- orig_beat = __get_spider_beat
955
+
960
956
already_in_time_warp = __system_thread_locals . get :sonic_pi_spider_in_time_warp
961
957
962
- __system_thread_locals . set ( :sonic_pi_spider_time_warp_start , vt_orig . freeze ) unless already_in_time_warp
958
+ __system_thread_locals . set ( :sonic_pi_spider_time_warp_start , __get_spider_time ) unless already_in_time_warp
963
959
__system_thread_locals . set_local :sonic_pi_spider_in_time_warp , true
960
+ __with_preserved_spider_time_and_beat do
964
961
965
- times . each_with_index do |delta , idx |
966
- sleep_time = delta * orig_sleep_mul_w_density
967
- new_time = vt_orig + sleep_time
962
+ times . each_with_index do |delta , idx |
968
963
969
- __change_spider_time! ( new_time )
970
- __change_spider_beat! ( orig_beat + delta )
971
- __system_thread_locals . set_local :sonic_pi_local_control_deltas , { }
972
- __system_thread_locals . set_local ( :sonic_pi_spider_time_state_cache , [ ] )
964
+ sleep delta
973
965
974
- case block . arity
975
- when 0
976
- block . call
977
- when 1
978
- block . call ( params [ idx % params_size ] )
979
- when 2
980
- if had_params
981
- block . call ( delta , params [ idx % params_size ] )
966
+ case block . arity
967
+ when 0
968
+ block . call
969
+ when 1
970
+ block . call ( params [ idx % params_size ] )
971
+ when 2
972
+ if had_params
973
+ block . call ( delta , params [ idx % params_size ] )
974
+ else
975
+ block . call ( delta , idx )
976
+ end
977
+ when 3
978
+ block . call ( t , params [ idx % params_size ] , idx )
982
979
else
983
- block . call ( delta , idx )
980
+ raise ArgumentError , " block for time_warp should only accept 0, 1, 2 or 3 parameters. You gave: #{ block . arity } ."
984
981
end
985
- when 3
986
- block . call ( t , params [ idx % params_size ] , idx )
987
- else
988
- raise ArgumentError , "block for time_warp should only accept 0, 1, 2 or 3 parameters. You gave: #{ block . arity } ."
982
+ __schedule_delayed_blocks_and_messages!
989
983
end
990
- __schedule_delayed_blocks_and_messages!
991
984
end
992
985
993
- __change_spider_time! ( vt_orig )
994
- __change_spider_beat! ( orig_beat )
995
986
__system_thread_locals . set_local :sonic_pi_spider_in_time_warp , already_in_time_warp
996
987
__system_thread_locals . set_local :sonic_pi_local_control_deltas , prev_ctl_deltas
997
988
__system_thread_locals . set_local ( :sonic_pi_spider_time_state_cache , prev_cache )
@@ -3573,8 +3564,7 @@ def with_cue_logging(v, &block)
3573
3564
def use_bpm ( bpm , &block )
3574
3565
raise ArgumentError , "use_bpm does not work with a block. Perhaps you meant with_bpm" if block
3575
3566
raise ArgumentError , "use_bpm's BPM should be a positive value. You tried to use: #{ bpm } " unless bpm > 0
3576
- sleep_mul = 60.0 / bpm
3577
- __change_spider_sleep_mul! ( sleep_mul )
3567
+ __change_spider_bpm! ( bpm )
3578
3568
end
3579
3569
doc name : :use_bpm ,
3580
3570
introduced : Version . new ( 2 , 0 , 0 ) ,
@@ -3627,11 +3617,10 @@ def use_bpm(bpm, &block)
3627
3617
def with_bpm ( bpm , &block )
3628
3618
raise ArgumentError , "with_bpm must be called with a do/end block. Perhaps you meant use_bpm" unless block
3629
3619
raise ArgumentError , "with_bpm's BPM should be a positive value. You tried to use: #{ bpm } " unless bpm > 0
3630
- current_mul = __get_spider_time
3631
- sleep_mul = 60.0 / bpm
3632
- __change_spider_sleep_mul! ( sleep_mul )
3620
+ current_bpm = __get_spider_bpm
3621
+ __change_spider_bpm! ( bpm )
3633
3622
res = block . call
3634
- __change_spider_sleep_mul! ( current_mul )
3623
+ __change_spider_bpm! ( curent_bpm )
3635
3624
res
3636
3625
end
3637
3626
doc name : :with_bpm ,
@@ -3685,11 +3674,11 @@ def with_bpm(bpm, &block)
3685
3674
def with_bpm_mul ( mul , &block )
3686
3675
raise ArgumentError , "with_bpm_mul must be called with a do/end block. Perhaps you meant use_bpm_mul" unless block
3687
3676
raise ArgumentError , "with_bpm_mul's mul should be a positive value. You tried to use: #{ mul } " unless mul > 0
3688
- current_mul = __get_spider_sleep_mul
3689
- new_mul = current_mul . to_f / mul
3690
- __change_spider_sleep_mul! ( new_mul )
3677
+ current_bpm = __get_spider_bpm
3678
+ new_bpm = current_bpm * mul . to_f
3679
+ __change_spider_bpm! ( new_bpm )
3691
3680
res = block . call
3692
- __change_spider_sleep_mul! ( current_mul )
3681
+ __change_spider_bpm! ( current_bpm )
3693
3682
res
3694
3683
end
3695
3684
doc name : :with_bpm_mul ,
@@ -3720,9 +3709,8 @@ def with_bpm_mul(mul, &block)
3720
3709
def use_bpm_mul ( mul , &block )
3721
3710
raise ArgumentError , "use_bpm_mul must not be called with a block. Perhaps you meant with_bpm_mul" if block
3722
3711
raise ArgumentError , "use_bpm_mul's mul should be a positive value. You tried to use: #{ mul } " unless mul > 0
3723
- current_mul = __get_spider_sleep_mul
3724
- new_mul = current_mul . to_f / mul
3725
- __change_spider_sleep_mul! ( new_mul )
3712
+ new_bpm = __get_spider_bpm * mul . to_f
3713
+ __change_spider_bpm! ( new_bpm )
3726
3714
end
3727
3715
doc name : :use_bpm_mul ,
3728
3716
introduced : Version . new ( 2 , 3 , 0 ) ,
@@ -4145,27 +4133,16 @@ def sleep(beats)
4145
4133
4146
4134
# Schedule messages
4147
4135
__schedule_delayed_blocks_and_messages!
4148
- __change_spider_beat! ( __get_spider_beat + beats )
4149
4136
return if beats == 0
4150
4137
4151
- # Grab the current virtual time
4152
- last_vt = __get_spider_time
4153
-
4154
- in_time_warp = __system_thread_locals . get ( :sonic_pi_spider_in_time_warp )
4155
-
4156
- # Now get on with syncing the rest of the sleep time...
4157
-
4158
- # Calculate the amount of time to sleep (take into account current bpm setting)
4159
- sleep_time = beats * __get_spider_sleep_mul
4160
-
4161
- # Calculate the new virtual time
4162
- new_vt = last_vt + sleep_time
4138
+ __change_spider_beat_and_time_by_beat_delta! ( beats )
4163
4139
4164
4140
sat = current_sched_ahead_time
4165
- __change_spider_time! ( new_vt )
4166
-
4141
+ new_vt = __get_spider_time
4167
4142
now = Time . now
4168
4143
4144
+ in_time_warp = __system_thread_locals . get ( :sonic_pi_spider_in_time_warp )
4145
+
4169
4146
if now - ( sat + 0.5 ) > new_vt
4170
4147
raise TimingError , "Timing Exception: thread got too far behind time"
4171
4148
elsif ( now - sat ) > new_vt
@@ -4193,9 +4170,10 @@ def sleep(beats)
4193
4170
end
4194
4171
else
4195
4172
if in_time_warp
4196
- # Don't sleep if within a time shift
4173
+ # Don't sleep if within a time warp
4174
+ #
4197
4175
# However, do make sure the vt hasn't got too far ahead of the real time
4198
- raise TimingError , "Timing Exception: thread got too far ahead of time" if ( new_vt - 17 ) > now
4176
+ # raise TimingError, "Timing Exception: thread got too far ahead of time" if (new_vt - 17) > now
4199
4177
else
4200
4178
Kernel . sleep new_vt - now
4201
4179
end
@@ -4338,14 +4316,14 @@ def sync_event(*args)
4338
4316
se = @event_history . sync ( t , p , i , d , b , m , cue_id , arg_matcher )
4339
4317
4340
4318
__system_thread_locals . set ( :sonic_pi_spider_synced , true )
4341
- __change_spider_beat! ( se . beat )
4342
4319
4343
- __change_spider_time! ( se . time )
4320
+ ## only need to use se for beat and time if not in :link or :metro bpm
4321
+ __change_spider_time_and_beat! ( se . time , se . beat )
4344
4322
__system_thread_locals . set_local :sonic_pi_local_last_sync , se
4345
4323
4346
4324
if bpm_sync
4347
4325
bpm = se . bpm <= 0 ? 60 : se . bpm
4348
- __change_spider_sleep_mul! ( 60.0 / bpm )
4326
+ use_bpm bpm
4349
4327
end
4350
4328
4351
4329
run_info = ""
0 commit comments