Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Updating for 4.0 Update #46

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .travis.yml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
language: rust
rust:
- 1.20.0
- 1.56.0
- stable
- beta
- nightly
Expand Down
57 changes: 39 additions & 18 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,11 @@ It's ideal for use with [sonicpi.vim][sonicpi.vim].

## Installation

### Prerequsities

- Sonic Pi >4.0
- Rust Compiler >1.56 (If installing from source)

### From source

If you have the [Rust programming language][rust-install] installed
Expand Down Expand Up @@ -49,14 +54,46 @@ Homebrew formula is here: [Cj-bc/homebrew-sonic-pi-tool](https://github.com/Cj-b

## Usage

- [start-server](#start-server)
- [check](#check)
- [eval](#eval)
- [eval-file](#eval-file)
- [eval-stdin](#eval-stdin)
- [stop](#stop)
- [logs](#logs)
- [start-server](#start-server)
- [record](#record)
- [sync](#sync)

### `start-server`

Attempts start the Sonic Pi server, if the executable can be found.

Not supported on Windows.

```sh
sonic-pi-tool start-server
# Sonic Pi server booting...
# Using protocol: udp
# Detecting port numbers...
# ...
```

`start-server` will write a `config.toml` under `~/.sonic-pi-tool` with the appropriate information required to connect to the sonic pi audio server.

### `sync`

The Sonic Pi server allocates ports dynamically, and also establishes a token to communicate amongst the different processes involved.

```sh
sonic-pi-tool sync 33926 1839294199
### Now sonic-pi-tool is sync'd with that server
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The user should be able to read this document and then know all need to know to be able to use the tool, so they'll need to know what numbers to use here.


sonic-pi-tool check
# => Sonic Pi server listening on port 33926
```

If you'd like to connect to the Sonic PI GUI please [see here](docs/SYNC-TO-SONIC-PI-GUI.md) for instructions on how to get the port and token.


### `check`

Expand All @@ -65,8 +102,7 @@ sonic-pi-tool check
# => Sonic Pi server listening on port 4557
```

Used to check if the Sonic Pi server is running. If the server isn't running
many of the tool's commands (such as `eval`) will not work.
Used to check if the Sonic Pi server is running on the port specified in the `~/.sonic-pi/tool/config.toml`. If the server isn't running or if the port isn't correctly configured in the toml file many of the tool's commands (such as `eval`) will not work.

This command returns a non-zero exit code if the server is not running.

Expand Down Expand Up @@ -131,21 +167,6 @@ sonic-pi-tool logs
# └ synth :beep, {note: 39.0, release: 0.1, amp: 0.9727}
```


### `start-server`

Attempts start the Sonic Pi server, if the executable can be found.

Not supported on Windows.

```sh
sonic-pi-tool start-server
# Sonic Pi server booting...
# Using protocol: udp
# Detecting port numbers...
# ...
```

### `record`

Record the audio output of a Sonic Pi session to a local file.
Expand Down
41 changes: 41 additions & 0 deletions docs/SYNC-TO-SONIC-PI-GUI.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
## Syncing sonic-pi-tool to a Runningi instance of Sonic PI GUI


## Why is this needed?

From version 4.0 and on, Sonic Pi's internal processes assing ports to communicate with one another dynamically (as opposed to using a pre-established one).
Additionally, they added the concept of a token which is required for messages to be processed by the audio server.

## How to sync?
If you want to use sonic-pi-tool with a running instance of Sonic Pi, you need the port of the audio server, and the token to make sure your messages are treated as valid.


### From Linux or Mac OS

From terminal run the following command:
```sh
> ps x | grep spider
```

This allows us to find the spider-server, which is the one that responds to the OSC commands sonic-pi-tool will send.
You should see something like:
```sh
> ps x | grep spider
52800 /Applications/Sonic Pi.app/Contents/Resources/app/server/native/ruby/bin/ruby
--enable-frozen-string-literal -E utf-8
/Applications/Sonic Pi.app/Contents/Resources/app/server/ruby/bin/spider-server.rb
-u 33926 33927 33928 33928 4560 33929 33933 1839294199
```

What we care about here are 2 numbers - The port of the server (the first number, right after the `-u` - `33926` in our example above) and the last number, which is the token (`1839294199` in the example above)

With those two number we can call `sonic-pi-tool sync` and it will take care of setting up the configuration required:
```sh
> sonic-pi-tool sync 33926 1839294199
## Now sonic-pi-tool is sync'd with that instance of Sonic Pi GUI

> sonic-pi-tool check
# => Sonic Pi server listening on port 33926
```

## From Windows
18 changes: 15 additions & 3 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -152,10 +152,8 @@ pub fn start_server() {

// Write the ports of the different processes into a config file so other commands know
// how to reach the right endpoints
std::fs::create_dir_all(&config::SonicPiToolCfg::get_default_cfg_folder()).unwrap();
let cur_cfg = &config::SonicPiToolCfg::new(token, sonic_pi_port, daemon_port, gui_port);
std::fs::write(&config::SonicPiToolCfg::get_default_cfg_file_path(),
toml::to_string(cur_cfg).unwrap()).unwrap();
write_config_toml(&cur_cfg);

loop {
server::send_keep_live();
Expand Down Expand Up @@ -187,3 +185,17 @@ pub fn record(path: &str) {
}
}
}

/// Sync with a Server that is already running (e.g. the Sonic Pi GUI)
/// Will write the Config file so that other commands can correctly talk to that server.
/// Refer to the README for more details on how to get the port and token
pub fn sync(sonic_pi_port:&u16, token: &i32) {
let cur_cfg = &config::SonicPiToolCfg::new(*token, *sonic_pi_port, 0, 0);
write_config_toml(&cur_cfg);
}

fn write_config_toml(cfg: &config::SonicPiToolCfg) {
std::fs::create_dir_all(&config::SonicPiToolCfg::get_default_cfg_folder()).unwrap();
std::fs::write(&config::SonicPiToolCfg::get_default_cfg_file_path(),
toml::to_string(cfg).unwrap()).unwrap();
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could you run the formatter please? Thank you

}
33 changes: 33 additions & 0 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,17 @@ fn main() {
.index(1),
);

let sync = SubCommand::with_name("sync")
.about("Sync with a running server by passing the port and token")
.args( &[ Arg::with_name("SERVER_PORT")
.help("Port at which the Scynth server is listening on")
.required(true)
.index(1),
Arg::with_name("TOKEN")
.help("")
.required(true)
.index(2)]);

let matches = cli_app
.subcommand(stop)
.subcommand(check)
Expand All @@ -62,6 +73,7 @@ fn main() {
.subcommand(eval_file)
.subcommand(start_server)
.subcommand(record)
.subcommand(sync)
.get_matches();

match matches.subcommand_name() {
Expand All @@ -73,6 +85,7 @@ fn main() {
Some("start-server") => lib::start_server(),
Some("logs") => lib::logs(),
Some("record") => do_record(&matches),
Some("sync") => do_sync(&matches),
_ => panic!("Unrecognised subcommand"), // This _should_ be unreachable
}
}
Expand Down Expand Up @@ -106,3 +119,23 @@ fn do_record(matches: &clap::ArgMatches) {
.to_string();
lib::record(&path);
}

fn do_sync(matches: &clap::ArgMatches) {
let sonic_pi_port = matches
.subcommand_matches("sync")
.unwrap()
.value_of("SERVER_PORT")
.unwrap()
.parse::<u16>()
.unwrap();

let token = matches
.subcommand_matches("sync")
.unwrap()
.value_of("TOKEN")
.unwrap()
.parse::<i32>()
.unwrap();
lib::sync(&sonic_pi_port, &token);
}