fake_sonic_pi 0.1.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/.rspec +3 -0
- data/CODE_OF_CONDUCT.md +84 -0
- data/Gemfile +9 -0
- data/Gemfile.lock +63 -0
- data/LICENSE.txt +21 -0
- data/README.md +163 -0
- data/Rakefile +9 -0
- data/fake_sonic_pi.gemspec +31 -0
- data/lib/fake_sonic_pi/events.rb +39 -0
- data/lib/fake_sonic_pi/rspec.rb +25 -0
- data/lib/fake_sonic_pi/version.rb +5 -0
- data/lib/fake_sonic_pi.rb +154 -0
- data/sig/fake_sonic_pi.rbs +4 -0
- metadata +58 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA256:
|
3
|
+
metadata.gz: d306b88cdd84d84172709495158c2a2f3f61b2c099b6a1d55b46121c6e12a6c1
|
4
|
+
data.tar.gz: d53eb547c51649cf9b661c5483bfeb0d345bc315d4791e1ec1aa6d3d4466b422
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: b8039570e4646317dfa352bd9ec8f5f53783658e19b854f8becf6a7146500901f5ee3dfdb9de1bf263031a8fc9c3e90b6d9e25053cae8d5f9d104dc07f81084e
|
7
|
+
data.tar.gz: ac3bbe806b6f8d3ac46f60df7ae3ecdd67fba167eced1943dfa933efeda2f9caf72866bda0dfac7b2f6c45bd288241eb9fb5380d797f5d9d2d3e13c9286f2f52
|
data/.rspec
ADDED
data/CODE_OF_CONDUCT.md
ADDED
@@ -0,0 +1,84 @@
|
|
1
|
+
# Contributor Covenant Code of Conduct
|
2
|
+
|
3
|
+
## Our Pledge
|
4
|
+
|
5
|
+
We as members, contributors, and leaders pledge to make participation in our community a harassment-free experience for everyone, regardless of age, body size, visible or invisible disability, ethnicity, sex characteristics, gender identity and expression, level of experience, education, socio-economic status, nationality, personal appearance, race, religion, or sexual identity and orientation.
|
6
|
+
|
7
|
+
We pledge to act and interact in ways that contribute to an open, welcoming, diverse, inclusive, and healthy community.
|
8
|
+
|
9
|
+
## Our Standards
|
10
|
+
|
11
|
+
Examples of behavior that contributes to a positive environment for our community include:
|
12
|
+
|
13
|
+
* Demonstrating empathy and kindness toward other people
|
14
|
+
* Being respectful of differing opinions, viewpoints, and experiences
|
15
|
+
* Giving and gracefully accepting constructive feedback
|
16
|
+
* Accepting responsibility and apologizing to those affected by our mistakes, and learning from the experience
|
17
|
+
* Focusing on what is best not just for us as individuals, but for the overall community
|
18
|
+
|
19
|
+
Examples of unacceptable behavior include:
|
20
|
+
|
21
|
+
* The use of sexualized language or imagery, and sexual attention or
|
22
|
+
advances of any kind
|
23
|
+
* Trolling, insulting or derogatory comments, and personal or political attacks
|
24
|
+
* Public or private harassment
|
25
|
+
* Publishing others' private information, such as a physical or email
|
26
|
+
address, without their explicit permission
|
27
|
+
* Other conduct which could reasonably be considered inappropriate in a
|
28
|
+
professional setting
|
29
|
+
|
30
|
+
## Enforcement Responsibilities
|
31
|
+
|
32
|
+
Community leaders are responsible for clarifying and enforcing our standards of acceptable behavior and will take appropriate and fair corrective action in response to any behavior that they deem inappropriate, threatening, offensive, or harmful.
|
33
|
+
|
34
|
+
Community leaders have the right and responsibility to remove, edit, or reject comments, commits, code, wiki edits, issues, and other contributions that are not aligned to this Code of Conduct, and will communicate reasons for moderation decisions when appropriate.
|
35
|
+
|
36
|
+
## Scope
|
37
|
+
|
38
|
+
This Code of Conduct applies within all community spaces, and also applies when an individual is officially representing the community in public spaces. Examples of representing our community include using an official e-mail address, posting via an official social media account, or acting as an appointed representative at an online or offline event.
|
39
|
+
|
40
|
+
## Enforcement
|
41
|
+
|
42
|
+
Instances of abusive, harassing, or otherwise unacceptable behavior may be reported to the community leaders responsible for enforcement at sgilperez@gmail.com. All complaints will be reviewed and investigated promptly and fairly.
|
43
|
+
|
44
|
+
All community leaders are obligated to respect the privacy and security of the reporter of any incident.
|
45
|
+
|
46
|
+
## Enforcement Guidelines
|
47
|
+
|
48
|
+
Community leaders will follow these Community Impact Guidelines in determining the consequences for any action they deem in violation of this Code of Conduct:
|
49
|
+
|
50
|
+
### 1. Correction
|
51
|
+
|
52
|
+
**Community Impact**: Use of inappropriate language or other behavior deemed unprofessional or unwelcome in the community.
|
53
|
+
|
54
|
+
**Consequence**: A private, written warning from community leaders, providing clarity around the nature of the violation and an explanation of why the behavior was inappropriate. A public apology may be requested.
|
55
|
+
|
56
|
+
### 2. Warning
|
57
|
+
|
58
|
+
**Community Impact**: A violation through a single incident or series of actions.
|
59
|
+
|
60
|
+
**Consequence**: A warning with consequences for continued behavior. No interaction with the people involved, including unsolicited interaction with those enforcing the Code of Conduct, for a specified period of time. This includes avoiding interactions in community spaces as well as external channels like social media. Violating these terms may lead to a temporary or permanent ban.
|
61
|
+
|
62
|
+
### 3. Temporary Ban
|
63
|
+
|
64
|
+
**Community Impact**: A serious violation of community standards, including sustained inappropriate behavior.
|
65
|
+
|
66
|
+
**Consequence**: A temporary ban from any sort of interaction or public communication with the community for a specified period of time. No public or private interaction with the people involved, including unsolicited interaction with those enforcing the Code of Conduct, is allowed during this period. Violating these terms may lead to a permanent ban.
|
67
|
+
|
68
|
+
### 4. Permanent Ban
|
69
|
+
|
70
|
+
**Community Impact**: Demonstrating a pattern of violation of community standards, including sustained inappropriate behavior, harassment of an individual, or aggression toward or disparagement of classes of individuals.
|
71
|
+
|
72
|
+
**Consequence**: A permanent ban from any sort of public interaction within the community.
|
73
|
+
|
74
|
+
## Attribution
|
75
|
+
|
76
|
+
This Code of Conduct is adapted from the [Contributor Covenant][homepage], version 2.0,
|
77
|
+
available at https://www.contributor-covenant.org/version/2/0/code_of_conduct.html.
|
78
|
+
|
79
|
+
Community Impact Guidelines were inspired by [Mozilla's code of conduct enforcement ladder](https://github.com/mozilla/diversity).
|
80
|
+
|
81
|
+
[homepage]: https://www.contributor-covenant.org
|
82
|
+
|
83
|
+
For answers to common questions about this code of conduct, see the FAQ at
|
84
|
+
https://www.contributor-covenant.org/faq. Translations are available at https://www.contributor-covenant.org/translations.
|
data/Gemfile
ADDED
data/Gemfile.lock
ADDED
@@ -0,0 +1,63 @@
|
|
1
|
+
PATH
|
2
|
+
remote: .
|
3
|
+
specs:
|
4
|
+
fake_sonic_pi (0.1.0)
|
5
|
+
|
6
|
+
GEM
|
7
|
+
remote: https://rubygems.org/
|
8
|
+
specs:
|
9
|
+
ast (2.4.2)
|
10
|
+
diff-lcs (1.5.0)
|
11
|
+
json (2.6.2)
|
12
|
+
parallel (1.22.1)
|
13
|
+
parser (3.1.2.1)
|
14
|
+
ast (~> 2.4.1)
|
15
|
+
rainbow (3.1.1)
|
16
|
+
rake (13.0.6)
|
17
|
+
regexp_parser (2.6.0)
|
18
|
+
rexml (3.2.5)
|
19
|
+
rspec (3.11.0)
|
20
|
+
rspec-core (~> 3.11.0)
|
21
|
+
rspec-expectations (~> 3.11.0)
|
22
|
+
rspec-mocks (~> 3.11.0)
|
23
|
+
rspec-core (3.11.0)
|
24
|
+
rspec-support (~> 3.11.0)
|
25
|
+
rspec-expectations (3.11.1)
|
26
|
+
diff-lcs (>= 1.2.0, < 2.0)
|
27
|
+
rspec-support (~> 3.11.0)
|
28
|
+
rspec-mocks (3.11.1)
|
29
|
+
diff-lcs (>= 1.2.0, < 2.0)
|
30
|
+
rspec-support (~> 3.11.0)
|
31
|
+
rspec-support (3.11.1)
|
32
|
+
rubocop (1.35.1)
|
33
|
+
json (~> 2.3)
|
34
|
+
parallel (~> 1.10)
|
35
|
+
parser (>= 3.1.2.1)
|
36
|
+
rainbow (>= 2.2.2, < 4.0)
|
37
|
+
regexp_parser (>= 1.8, < 3.0)
|
38
|
+
rexml (>= 3.2.5, < 4.0)
|
39
|
+
rubocop-ast (>= 1.20.1, < 2.0)
|
40
|
+
ruby-progressbar (~> 1.7)
|
41
|
+
unicode-display_width (>= 1.4.0, < 3.0)
|
42
|
+
rubocop-ast (1.22.0)
|
43
|
+
parser (>= 3.1.1.0)
|
44
|
+
rubocop-performance (1.14.3)
|
45
|
+
rubocop (>= 1.7.0, < 2.0)
|
46
|
+
rubocop-ast (>= 0.4.0)
|
47
|
+
ruby-progressbar (1.11.0)
|
48
|
+
standard (1.16.1)
|
49
|
+
rubocop (= 1.35.1)
|
50
|
+
rubocop-performance (= 1.14.3)
|
51
|
+
unicode-display_width (2.3.0)
|
52
|
+
|
53
|
+
PLATFORMS
|
54
|
+
x86_64-linux
|
55
|
+
|
56
|
+
DEPENDENCIES
|
57
|
+
fake_sonic_pi!
|
58
|
+
rake
|
59
|
+
rspec
|
60
|
+
standard
|
61
|
+
|
62
|
+
BUNDLED WITH
|
63
|
+
2.3.11
|
data/LICENSE.txt
ADDED
@@ -0,0 +1,21 @@
|
|
1
|
+
The MIT License (MIT)
|
2
|
+
|
3
|
+
Copyright (c) 2022 Sergio Gil
|
4
|
+
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
7
|
+
in the Software without restriction, including without limitation the rights
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
10
|
+
furnished to do so, subject to the following conditions:
|
11
|
+
|
12
|
+
The above copyright notice and this permission notice shall be included in
|
13
|
+
all copies or substantial portions of the Software.
|
14
|
+
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
21
|
+
THE SOFTWARE.
|
data/README.md
ADDED
@@ -0,0 +1,163 @@
|
|
1
|
+
# FakeSonicPi
|
2
|
+
|
3
|
+
`FakeSonicPi` is a small utility class used to test Sonic Pi related code. It
|
4
|
+
implements a tiny subset of its API in a silent (yep, no sound) and
|
5
|
+
instant-running (as opposed to the real Sonic Pi, which is supposed to keep
|
6
|
+
running indefinitely). It was originally part of the test suite of
|
7
|
+
[sonic-pi-akai-apc-mini](https://github.com/porras/sonic-pi-akai-apc-mini), so
|
8
|
+
it implements only a very small part of the Sonic Pi API, focusing on timing
|
9
|
+
(being able to check what happens at which time). Since extracting, I've also
|
10
|
+
used it to test [ptn](https://github.com/porras/ptn) and added a couple of
|
11
|
+
features needed for it.
|
12
|
+
|
13
|
+
If that doesn't make much sense, have a look at the examples in this README, the
|
14
|
+
tests in `spec/`, and the tests of the projects that use it.
|
15
|
+
|
16
|
+
## Installation
|
17
|
+
|
18
|
+
Install the gem and add to the library's Gemfile by executing:
|
19
|
+
|
20
|
+
$ bundle add fake_sonic_pi
|
21
|
+
|
22
|
+
## Usage
|
23
|
+
|
24
|
+
You need to start by requiring it in your test, your `test_helper.rb`, or
|
25
|
+
`spec_helper.rb`. Then, you can use it to define a Sonic Pi program, in a block.
|
26
|
+
That would be what you would type on a Sonic Pi buffer:
|
27
|
+
|
28
|
+
```ruby
|
29
|
+
sp = FakeSonicPi.new do
|
30
|
+
live_loop :drums do
|
31
|
+
sample :bd_haus
|
32
|
+
sleep 0.5
|
33
|
+
end
|
34
|
+
end
|
35
|
+
```
|
36
|
+
|
37
|
+
Then, you can _run_ it (you don't really run it but **simulate** it) for a number of beats:
|
38
|
+
|
39
|
+
```ruby
|
40
|
+
sp.run(2)
|
41
|
+
```
|
42
|
+
|
43
|
+
After doing that, the `sp` object has an `output`, which contains the sounds
|
44
|
+
(and other events that your code has produced), associated to the timing where
|
45
|
+
they happened:
|
46
|
+
|
47
|
+
```
|
48
|
+
#<FakeSonicPi::Events:0x000055ac84368978
|
49
|
+
@events=
|
50
|
+
[[0.0,
|
51
|
+
#<struct FakeSonicPi::Events::Event
|
52
|
+
name=:sample,
|
53
|
+
value=[:bd_haus],
|
54
|
+
processed_by=#<Set: {}>>],
|
55
|
+
[0.5,
|
56
|
+
#<struct FakeSonicPi::Events::Event
|
57
|
+
name=:sample,
|
58
|
+
value=[:bd_haus],
|
59
|
+
processed_by=#<Set: {}>>],
|
60
|
+
[1.0,
|
61
|
+
#<struct FakeSonicPi::Events::Event
|
62
|
+
name=:sample,
|
63
|
+
value=[:bd_haus],
|
64
|
+
processed_by=#<Set: {}>>],
|
65
|
+
[1.5,
|
66
|
+
#<struct FakeSonicPi::Events::Event
|
67
|
+
name=:sample,
|
68
|
+
value=[:bd_haus],
|
69
|
+
processed_by=#<Set: {}>>],
|
70
|
+
[2.0,
|
71
|
+
#<struct FakeSonicPi::Events::Event
|
72
|
+
name=:sample,
|
73
|
+
value=[:bd_haus],
|
74
|
+
processed_by=#<Set: {}>>]]>
|
75
|
+
```
|
76
|
+
|
77
|
+
You can use this object to check that your code did what it should. The internal
|
78
|
+
structure is as follows (as you can see in the inspect output above):
|
79
|
+
|
80
|
+
* It has an `events` attribute, which is an array of pairs
|
81
|
+
* In each pair:
|
82
|
+
* The first element is a `Float`, referencing the beat in which the event happened.
|
83
|
+
* The second one is an `Event` object, with the following attributes:
|
84
|
+
* `name`: the type of event: `:play`, `:sample`, `:midi_note_on`, ...
|
85
|
+
(basically the Sonic Pi command that was called).
|
86
|
+
* `value`: an array with the arguments passed to that command (the sample
|
87
|
+
name, the note, etc.).
|
88
|
+
|
89
|
+
So you can check that your example produced one `:bd_haus` sound each half beat with something like this:
|
90
|
+
|
91
|
+
```ruby
|
92
|
+
assert_equal 5, sp.output.events.size
|
93
|
+
|
94
|
+
beat, event = sp.output.events[0]
|
95
|
+
assert_equal 0.0, beat
|
96
|
+
assert_equal :sample, event.name
|
97
|
+
assert_equal :bd_haus, event.value.first
|
98
|
+
```
|
99
|
+
|
100
|
+
### RSpec
|
101
|
+
|
102
|
+
If you use RSpec, a matcher is provided to make such assertions simpler. Require
|
103
|
+
`fake_sonic_pi/rspec` in your `spec_helper.rb` and you can do the following:
|
104
|
+
|
105
|
+
```ruby
|
106
|
+
expect(sp).to have_output(:sample, :bd_haus).at(0, 0.5, 1, 1.5, 2)
|
107
|
+
```
|
108
|
+
|
109
|
+
Check the `spec/` directory for more examples.
|
110
|
+
|
111
|
+
### Implemented subset / Limitations
|
112
|
+
|
113
|
+
The following commands of the API are implemented:
|
114
|
+
|
115
|
+
* `play`
|
116
|
+
* `sample`
|
117
|
+
* `sleep`
|
118
|
+
* `at`
|
119
|
+
* `midi_note_on` / `midi_note_off`
|
120
|
+
* `get` / `set` / `cue` / `sync`
|
121
|
+
* `stop`
|
122
|
+
* `in_thread` (but not completely, so depending on what you do in the thread, it might not work as expected)
|
123
|
+
* `live_loop` (but not the `sync` option)
|
124
|
+
* `with_fx`
|
125
|
+
* `control`
|
126
|
+
* `set_volume!`
|
127
|
+
|
128
|
+
The main limitation is that several commands (prominently `sleep`) don't work if
|
129
|
+
they're not inside a `live_loop`. This is quite different of the real Sonic Pi,
|
130
|
+
where such limitation doesn't exist, but it is in general possible to write your
|
131
|
+
tests using `live_loop`s, so, at least for the libraries I've used FakeSonicPi
|
132
|
+
with, it is not such a big deal.
|
133
|
+
|
134
|
+
## Development
|
135
|
+
|
136
|
+
After checking out the repo, run `bin/setup` to install dependencies. Then, run
|
137
|
+
`rake spec` to run the tests. You can also run `bin/console` for an interactive
|
138
|
+
prompt that will allow you to experiment.
|
139
|
+
|
140
|
+
To install this gem onto your local machine, run `bundle exec rake install`. To
|
141
|
+
release a new version, update the version number in `version.rb`, and then run
|
142
|
+
`bundle exec rake release`, which will create a git tag for the version, push
|
143
|
+
git commits and the created tag, and push the `.gem` file to
|
144
|
+
[rubygems.org](https://rubygems.org).
|
145
|
+
|
146
|
+
## Contributing
|
147
|
+
|
148
|
+
Bug reports and pull requests are welcome on GitHub at
|
149
|
+
https://github.com/porras/fake_sonic_pi. This project is intended to be a safe,
|
150
|
+
welcoming space for collaboration, and contributors are expected to adhere to
|
151
|
+
the [code of
|
152
|
+
conduct](https://github.com/porras/fake_sonic_pi/blob/main/CODE_OF_CONDUCT.md).
|
153
|
+
|
154
|
+
## License
|
155
|
+
|
156
|
+
The gem is available as open source under the terms of the [MIT
|
157
|
+
License](https://opensource.org/licenses/MIT).
|
158
|
+
|
159
|
+
## Code of Conduct
|
160
|
+
|
161
|
+
Everyone interacting in the FakeSonicPi project's codebases, issue trackers,
|
162
|
+
chat rooms and mailing lists is expected to follow the [code of
|
163
|
+
conduct](https://github.com/porras/fake_sonic_pi/blob/main/CODE_OF_CONDUCT.md).
|
data/Rakefile
ADDED
@@ -0,0 +1,31 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
require_relative "lib/fake_sonic_pi/version"
|
4
|
+
|
5
|
+
Gem::Specification.new do |spec|
|
6
|
+
spec.name = "fake_sonic_pi"
|
7
|
+
spec.version = FakeSonicPi::VERSION
|
8
|
+
spec.authors = ["Sergio Gil"]
|
9
|
+
spec.email = ["sgilperez@gmail.com"]
|
10
|
+
|
11
|
+
spec.summary = "Support library to write tests for Sonic Pi code"
|
12
|
+
spec.description = "fake_sonic_pi is a reimplementation of a small subset of the Sonic Pi"
|
13
|
+
spec.homepage = "https://github.com/porras/fake_sonic_pi"
|
14
|
+
spec.license = "MIT"
|
15
|
+
spec.required_ruby_version = ">= 2.6.0"
|
16
|
+
|
17
|
+
spec.metadata["homepage_uri"] = spec.homepage
|
18
|
+
spec.metadata["source_code_uri"] = spec.homepage
|
19
|
+
# spec.metadata["changelog_uri"] = "TODO: Put your gem's CHANGELOG.md URL here."
|
20
|
+
|
21
|
+
# Specify which files should be added to the gem when it is released.
|
22
|
+
# The `git ls-files -z` loads the files in the RubyGem that have been added into git.
|
23
|
+
spec.files = Dir.chdir(__dir__) do
|
24
|
+
`git ls-files -z`.split("\x0").reject do |f|
|
25
|
+
(f == __FILE__) || f.match(%r{\A(?:(?:bin|test|spec|features)/|\.(?:git|travis|circleci)|appveyor)})
|
26
|
+
end
|
27
|
+
end
|
28
|
+
spec.bindir = "exe"
|
29
|
+
spec.executables = spec.files.grep(%r{\Aexe/}) { |f| File.basename(f) }
|
30
|
+
spec.require_paths = ["lib"]
|
31
|
+
end
|
@@ -0,0 +1,39 @@
|
|
1
|
+
class FakeSonicPi
|
2
|
+
class Events
|
3
|
+
attr_reader :events
|
4
|
+
|
5
|
+
Event = Struct.new(:name, :value, :processed_by) do
|
6
|
+
def initialize(name, value)
|
7
|
+
super(name, value, Set.new)
|
8
|
+
end
|
9
|
+
end
|
10
|
+
|
11
|
+
def initialize
|
12
|
+
@events = []
|
13
|
+
end
|
14
|
+
|
15
|
+
def most_recent(beat, name)
|
16
|
+
@events.select { |b, e| b <= beat && e.name == name }.max_by(&:first)&.last
|
17
|
+
end
|
18
|
+
|
19
|
+
def next_beat(beat)
|
20
|
+
@events.map(&:first).sort.find { |b| b > beat }
|
21
|
+
end
|
22
|
+
|
23
|
+
def find(beat, name)
|
24
|
+
@events.find { |b, e| b == beat && e.name == name && !e.processed_by.include?(Fiber.current) }&.last
|
25
|
+
end
|
26
|
+
|
27
|
+
def add_batch(events)
|
28
|
+
events.each { |b, n, v| add b, n, v }
|
29
|
+
end
|
30
|
+
|
31
|
+
def add(beat, name, value)
|
32
|
+
@events << [beat, Event.new(name, value)]
|
33
|
+
end
|
34
|
+
|
35
|
+
def ==(other)
|
36
|
+
other.is_a?(self.class) && @events == other.events
|
37
|
+
end
|
38
|
+
end
|
39
|
+
end
|
@@ -0,0 +1,25 @@
|
|
1
|
+
require "rspec/matchers"
|
2
|
+
require_relative "../fake_sonic_pi"
|
3
|
+
|
4
|
+
RSpec::Matchers.define :have_output do |command, *args|
|
5
|
+
match do |sp|
|
6
|
+
expect(@beats).to all(satisfy do |beat|
|
7
|
+
beat_commands = sp.output.events.select { |b, *_| b == beat }.map(&:last)
|
8
|
+
expect(beat_commands).to include(have_attributes(name: command, value: args))
|
9
|
+
end)
|
10
|
+
end
|
11
|
+
|
12
|
+
chain :at do |*beats|
|
13
|
+
@beats = beats.map(&:to_f)
|
14
|
+
end
|
15
|
+
end
|
16
|
+
|
17
|
+
# TODO: The matching is very simplistic, ok for now but might need some refining
|
18
|
+
# to be able to pass other matchers.
|
19
|
+
RSpec::Matchers.define :a_node do |command, arg|
|
20
|
+
match do |actual|
|
21
|
+
expect(actual).to be_a(FakeSonicPi::Node)
|
22
|
+
expect(actual.command).to eq command
|
23
|
+
expect(actual.args.first).to eq(arg)
|
24
|
+
end
|
25
|
+
end
|
@@ -0,0 +1,154 @@
|
|
1
|
+
require "fiber" # for Ruby < 3.0
|
2
|
+
require "set"
|
3
|
+
require_relative "fake_sonic_pi/version"
|
4
|
+
require_relative "fake_sonic_pi/events"
|
5
|
+
|
6
|
+
class FakeSonicPi
|
7
|
+
class NoSleep < StandardError; end
|
8
|
+
|
9
|
+
attr_reader :output
|
10
|
+
|
11
|
+
def initialize(&definition)
|
12
|
+
@definition = definition
|
13
|
+
@output = Events.new
|
14
|
+
@events = Events.new
|
15
|
+
@beat = 0.0
|
16
|
+
@fibers = {}
|
17
|
+
end
|
18
|
+
|
19
|
+
# MAGIC :D I mean Fibers ;)
|
20
|
+
def run(beats, events: [])
|
21
|
+
@events.add_batch(events)
|
22
|
+
instance_eval(&@definition)
|
23
|
+
loop do
|
24
|
+
# remove terminated fibers (`at` blocks that already ran)
|
25
|
+
@fibers.select! { |f, _b| f.alive? }
|
26
|
+
|
27
|
+
# split fibers waiting for an event, and fibers scheduled for a particular
|
28
|
+
# beat (sleeping or scheduled with `at`)
|
29
|
+
waiting_fibers, scheduled_fibers = @fibers.partition { |_f, b| b.nil? }
|
30
|
+
|
31
|
+
# from the scheduled ones, remove those scheduled for after the max number of beats
|
32
|
+
scheduled_fibers.reject! { |_f, b| b > beats }
|
33
|
+
|
34
|
+
# give all waiting fibers a chance
|
35
|
+
events_before = @events.dup
|
36
|
+
waiting_fibers.each do |f, _b|
|
37
|
+
@fibers[f] = f.resume
|
38
|
+
end
|
39
|
+
# if any of them added an event, do it again
|
40
|
+
next if events_before != @events
|
41
|
+
|
42
|
+
# find next scheduled fiber (and for when is it scheduled?)
|
43
|
+
next_fiber, next_beat = scheduled_fibers.min_by { |_f, beat| beat }
|
44
|
+
|
45
|
+
# is there any event to happen before next_beat? if so, process that before
|
46
|
+
next_beat_with_event = @events.next_beat(@beat)
|
47
|
+
if next_beat_with_event && (next_beat.nil? || next_beat_with_event < next_beat)
|
48
|
+
@beat = next_beat_with_event
|
49
|
+
next
|
50
|
+
# otherwise proceed with the next scheduled fiber
|
51
|
+
elsif next_fiber
|
52
|
+
@beat = next_beat
|
53
|
+
@fibers[next_fiber] = next_fiber.resume
|
54
|
+
# and if there is none, then we're done \o/
|
55
|
+
else
|
56
|
+
break
|
57
|
+
end
|
58
|
+
end
|
59
|
+
end
|
60
|
+
|
61
|
+
def live_loop(name, &block)
|
62
|
+
# create a fiber that runs the block repeatedly...
|
63
|
+
f = Fiber.new do
|
64
|
+
loop do
|
65
|
+
Thread.current[:slept] = false
|
66
|
+
block.call
|
67
|
+
raise NoSleep, "live_loop #{name} didn't sleep" unless Thread.current[:slept]
|
68
|
+
end
|
69
|
+
end
|
70
|
+
# ...and schedule it for now
|
71
|
+
@fibers[f] = @beat
|
72
|
+
end
|
73
|
+
|
74
|
+
# sleep the fast way ;)
|
75
|
+
def sleep(n)
|
76
|
+
Thread.current[:slept] = true
|
77
|
+
Fiber.yield @beat + n
|
78
|
+
end
|
79
|
+
|
80
|
+
def at(*beats, &block)
|
81
|
+
# for each specified beat, create a fiber that calls the block once, and
|
82
|
+
# schedule it for then.
|
83
|
+
beats.each do |beat|
|
84
|
+
f = Fiber.new(&block)
|
85
|
+
@fibers[f] = @beat + beat
|
86
|
+
end
|
87
|
+
end
|
88
|
+
|
89
|
+
def sync(event_name)
|
90
|
+
loop do
|
91
|
+
Thread.current[:slept] = true
|
92
|
+
# find event in current beat and return its value, otherwise let the other
|
93
|
+
# fibers progress, then try again
|
94
|
+
if (event = @events.find(@beat, event_name))
|
95
|
+
event.processed_by << Fiber.current
|
96
|
+
return event.value
|
97
|
+
else
|
98
|
+
Fiber.yield nil
|
99
|
+
end
|
100
|
+
end
|
101
|
+
end
|
102
|
+
|
103
|
+
def get(name, default = nil)
|
104
|
+
if (event = @events.most_recent(@beat, name))
|
105
|
+
event.value
|
106
|
+
else
|
107
|
+
default
|
108
|
+
end
|
109
|
+
end
|
110
|
+
|
111
|
+
def set(name, value)
|
112
|
+
@events.add(@beat, name, value)
|
113
|
+
end
|
114
|
+
|
115
|
+
alias_method :cue, :set
|
116
|
+
|
117
|
+
def in_thread
|
118
|
+
# just do it ;) fibers are awesome :D
|
119
|
+
yield
|
120
|
+
end
|
121
|
+
|
122
|
+
def stop
|
123
|
+
# kind of stop :D
|
124
|
+
sleep Float::INFINITY
|
125
|
+
end
|
126
|
+
|
127
|
+
# commands we store as output, returning a (fake) node
|
128
|
+
%i[play sample synth control midi_note_on set_volume!].each do |command|
|
129
|
+
define_method(command) do |*args|
|
130
|
+
@output.add @beat, command, args
|
131
|
+
Node.new(command, args)
|
132
|
+
end
|
133
|
+
end
|
134
|
+
|
135
|
+
def with_fx(*args, &block)
|
136
|
+
block.call(Node.new(:fx, args))
|
137
|
+
end
|
138
|
+
|
139
|
+
Node = Struct.new(:command, :args) do
|
140
|
+
def kill
|
141
|
+
# no-op
|
142
|
+
end
|
143
|
+
end
|
144
|
+
|
145
|
+
# no-ops (sonic pi commands whose effect is not relevant here, but need to be
|
146
|
+
# implemented so that the test doesn't fail)
|
147
|
+
%i[use_real_time].each do |cmd|
|
148
|
+
define_method(cmd) { |*_args| }
|
149
|
+
end
|
150
|
+
|
151
|
+
def include(*args)
|
152
|
+
self.class.include(*args)
|
153
|
+
end
|
154
|
+
end
|
metadata
ADDED
@@ -0,0 +1,58 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: fake_sonic_pi
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.1.0
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Sergio Gil
|
8
|
+
autorequire:
|
9
|
+
bindir: exe
|
10
|
+
cert_chain: []
|
11
|
+
date: 2022-10-20 00:00:00.000000000 Z
|
12
|
+
dependencies: []
|
13
|
+
description: fake_sonic_pi is a reimplementation of a small subset of the Sonic Pi
|
14
|
+
email:
|
15
|
+
- sgilperez@gmail.com
|
16
|
+
executables: []
|
17
|
+
extensions: []
|
18
|
+
extra_rdoc_files: []
|
19
|
+
files:
|
20
|
+
- ".rspec"
|
21
|
+
- CODE_OF_CONDUCT.md
|
22
|
+
- Gemfile
|
23
|
+
- Gemfile.lock
|
24
|
+
- LICENSE.txt
|
25
|
+
- README.md
|
26
|
+
- Rakefile
|
27
|
+
- fake_sonic_pi.gemspec
|
28
|
+
- lib/fake_sonic_pi.rb
|
29
|
+
- lib/fake_sonic_pi/events.rb
|
30
|
+
- lib/fake_sonic_pi/rspec.rb
|
31
|
+
- lib/fake_sonic_pi/version.rb
|
32
|
+
- sig/fake_sonic_pi.rbs
|
33
|
+
homepage: https://github.com/porras/fake_sonic_pi
|
34
|
+
licenses:
|
35
|
+
- MIT
|
36
|
+
metadata:
|
37
|
+
homepage_uri: https://github.com/porras/fake_sonic_pi
|
38
|
+
source_code_uri: https://github.com/porras/fake_sonic_pi
|
39
|
+
post_install_message:
|
40
|
+
rdoc_options: []
|
41
|
+
require_paths:
|
42
|
+
- lib
|
43
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
44
|
+
requirements:
|
45
|
+
- - ">="
|
46
|
+
- !ruby/object:Gem::Version
|
47
|
+
version: 2.6.0
|
48
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
49
|
+
requirements:
|
50
|
+
- - ">="
|
51
|
+
- !ruby/object:Gem::Version
|
52
|
+
version: '0'
|
53
|
+
requirements: []
|
54
|
+
rubygems_version: 3.2.32
|
55
|
+
signing_key:
|
56
|
+
specification_version: 4
|
57
|
+
summary: Support library to write tests for Sonic Pi code
|
58
|
+
test_files: []
|