File: test.rb

package info (click to toggle)
sonic-pi 3.2.2~repack-8
  • links: PTS, VCS
  • area: main
  • in suites: bookworm
  • size: 71,872 kB
  • sloc: ruby: 30,548; cpp: 8,490; sh: 957; ansic: 461; erlang: 360; lisp: 141; makefile: 44
file content (179 lines) | stat: -rwxr-xr-x 4,335 bytes parent folder | download | duplicates (4)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
#!/usr/bin/env ruby
#--
# This file is part of Sonic Pi: http://sonic-pi.net
# Full project source: https://github.com/samaaron/sonic-pi
# License: https://github.com/samaaron/sonic-pi/blob/master/LICENSE.md
#
# Copyright 2013, 2014, 2015, 2016 by Sam Aaron (http://sam.aaron.name).
# All rights reserved.
#
# Permission is granted for use, copying, modification, and
# distribution of modified versions of this work as long as this
# notice is included.
#++

require_relative "../core.rb"
require_relative "../lib/sonicpi/studio"
require_relative "../lib/sonicpi/spider"
require_relative "../lib/sonicpi/spiderapi"
require_relative "../lib/sonicpi/server"
require_relative "../lib/sonicpi/util"
require_relative "../lib/sonicpi/rcv_dispatch"

#Thread.abort_on_exception=true

include SonicPi::Util

ws_out = Queue.new
$scsynth = SonicPi::SCSynth.instance

$c = OSC::Client.new("127.0.0.1", 4556)

at_exit do
  $c.send(OSC::Message.new("/quit"))
end


$c.send(OSC::Message.new("/d_loadDir", synthdef_path))
sleep 2

user_methods = Module.new
name = "SonicPiSpiderUser1" # this should be autogenerated
klass = Object.const_set name, Class.new(SonicPi::Spider)
klass.send(:include, user_methods)
klass.send(:include, SonicPi::SpiderAPI)
$sp =  klass.new "127.0.0.1", 4556, ws_out, 5, user_methods
$rd = SonicPi::RcvDispatch.new($sp, ws_out)

$clients = []

# Send stuff out from Sonic Pi jobs out to GUI
out_t = Thread.new do
  continue = true
  while continue
    begin
      message = ws_out.pop
      message[:ts] = Time.now.strftime("%H:%M:%S")

      if message[:type] == :exit
        continue = false
      else
        puts message
      end
    rescue Exception => e
      puts "Exception!"
      puts e.message
      puts e.backtrace.inspect
    end
  end
end

Thread.new do
  f = File.open("/tmp/gc.txt", 'w')
  loop do
    f.puts GC.stat
    f.flush
    sleep 2
  end
end

def test_simple
  $rd.dispatch({:cmd => "run-code",
                :val => "play 60"})
end

def test_multi_osc
  $rd.dispatch({:cmd => "run-code",
                :val => "loop do ; status ; sleep 0.025 ; end"})
end

def test_multi_play
  $rd.dispatch({:cmd => "run-code",
                :val => "loop do ; play 60 ; sleep 0.025 ; end"})
end

def test_multi_threads
  $rd.dispatch({:cmd => "run-code",
                :val => "loop do ; in_thread do ; play 60 ; sleep 3 ; end ; sleep 0.025 ; end"})
end

def test_multi_similarly_named_threads
  $rd.dispatch({:cmd => "run-code",
                :val => "loop do ; in_thread(name: :foo) do ; play 60 ; sleep 3 ; end ; sleep 0.025 ; end"})
end

def test_multi_differently_named_threads
  $rd.dispatch({:cmd => "run-code",
                :val => "loop do ; in_thread(name: rand) do ; play 60 ; sleep 3 ; end ; sleep 0.025 ; end"})
end

def test_multi_inner_threads
  $rd.dispatch({:cmd => "run-code",
                :val => "loop do ; in_thread do ; in_thread do ; play 60 ; end ; end ; sleep 0.025 ; end"})
end

def test_multi_jobs
  loop do
    $rd.dispatch({:cmd => "run-code",
                  :val => "play 60"})
    sleep 0.025
  end
end

def test_multi_with_fx
  $rd.dispatch({:cmd => "run-code",
                :val => "loop do ; with_fx :slicer do ; play 60 ; sleep 0.025 ; end ; end"})
end

def test_stopping_within_fx_block
  loop do
    $rd.dispatch({:cmd => "run-code",
                  :val => "with_fx do ; loop do ; play 60 ; sleep 5 ; end ; end"})
    sleep 1
    $rd.dispatch({:cmd => "stop-jobs"})
    sleep 1
  end
end

def test_exception_throwing
  loop do
    $rd.dispatch({:cmd => "run-code",
                  :val => "play 60 ; 1/0"})
    sleep 0.025
  end
end

def test_exception_throwing_within_subthread
  loop do
    $rd.dispatch({:cmd => "run-code",
                  :val => "play 60 ; in_thread do ; 1/0 ; end"})
    sleep 0.025
  end
end

def test_all_jobs_stopping
  loop do
    $rd.dispatch({:cmd => "run-code",
                   :val => "loop do ; play 60 ; sleep 0.025 ; end"})
    sleep 3
    $rd.dispatch({:cmd => "stop-jobs"})
    sleep 1
  end
end

#test_simple
#test_multi_osc
#test_multi_play
#test_multi_threads
#test_multi_similarly_named_threads
#test_multi_differently_named_threads
#test_multi_inner_threads
#test_multi_jobs
test_multi_with_fx
#test_exception_throwing
#test_exception_throwing_within_subthread
#test_all_jobs_stopping



out_t.join