Skip to content

Commit

Permalink
Use Node script for filesystem operations (#24)
Browse files Browse the repository at this point in the history
  • Loading branch information
delucis authored Jan 23, 2023
1 parent dd26680 commit 5a7d747
Show file tree
Hide file tree
Showing 12 changed files with 1,731 additions and 2,434 deletions.
15 changes: 10 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,16 +11,16 @@ A modular kit providing high-level sound playback, processing and performance to
1. **Download the [latest release](https://github.com/mus264/264-tools/releases/latest)** (click ‘Source code’ under Assets)

2. **Uncompress your download to your Max Packages directory.**
You can find this under `~/Documents/Max 7/Packages` for Max 7
You should end up with file paths that look like `~/Documents/Max 7/Packages/264-tools/ALL-THE-FILES-GO-HERE`.
You can find this under `~/Documents/Max 8/Packages` for Max 8
You should end up with file paths that look like `~/Documents/Max 8/Packages/264-tools/ALL-THE-FILES-GO-HERE`.

3. **Open (or restart) Max.**

Alternatively if you prefer using the command line:

```sh
# move to your packages directory
cd ~/Documents/Max 7/Packages
cd ~/Documents/Max 8/Packages
# download 264 Tools to your packages directory
git clone https://github.com/mus264/264-tools.git
```
Expand Down Expand Up @@ -83,13 +83,18 @@ The toolkit currently includes the following modules, which should be loaded in

## Compatibility

These modules have been tested with Max 6 and 7. They will not work with Max/MSP 5 or lower. Please [report bugs under the issues tab above](https://github.com/mus264/264-tools/issues).
The latest release of these modules requires **Max 8**. Please [report bugs under the issues tab above][issues].

If you need to support Max 6 or 7, you can [download v0.17.0][017] or lower.

[issues]: https://github.com/mus264/264-tools/issues
[017]: https://github.com/mus264/264-tools/releases/tag/v0.17.0

## Acknowledgments

`264.grains~` relies on the `munger~` granulation external, which has a substantial ancestry including work by Ivica Ico Bukvic, Ji-Sun Kim, Dan Trueman, and R. Luke DuBois, most recently for [percolate](https://github.com/Cycling74/percolate).

`264.midi-presets`, `264.audio-presets`, `264.sfplay~`, and `264.sfrecord~` rely on [Patrick Delges](http://www.crfmw.be/max/)’s `filesys` Java class to manage file locations.
The original versions of `264.midi-presets`, `264.audio-presets`, `264.loop~`, `264.sfplay~`, and `264.sfrecord~` relied on [Patrick Delges](http://www.crfmw.be/max/)’s `filesys` Java class to manage file locations. (This is now done via a custom Node.js script.)

The `264.reverb~` core is heavily based on [Randy Jones](http://madronalabs.com/)’s `yafr2` example.

Expand Down
1,449 changes: 0 additions & 1,449 deletions help/filesys.maxhelp

This file was deleted.

Binary file removed java-classes/filesys.class
Binary file not shown.
100 changes: 0 additions & 100 deletions java-classes/filesys.java

This file was deleted.

95 changes: 95 additions & 0 deletions javascript/264.fs.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,95 @@
// ---------------------------------------------------------------------------
//
// 264.fs.js
//
// Provide `exists` and `mkdir` filesystem functions to replace the
// filesys Java class by Patrick Delges previously used by 264 Tools.
//
// ---------------------------------------------------------------------------

const { existsSync } = require('fs');
const { mkdir, copyFile } = require('fs').promises;
const os = require('os');
const maxAPI = require('max-api');
const { resolve } = require('path');

// Script success/failure codes.
const FAILURE = 0;
const SUCCESS = 1;

// Register handlers with Max.
maxAPI.addHandlers({
exists: withErrorHandling('exists', exists),
mkdir: withErrorHandling('mkdir', makeDirectory),
cp: withErrorHandling('cp', cp),
});

/**
* Higher-order function to wrap a handler with basic error handling.
* @param {string} commandName Command name.
* @param {(...paths: string[]) => Promise<void>} handler Handler to wrap.
* @returns {(...paths: string[]) => Promise<void>} An async function that will catch and log errors from the wrapped handler.
*/
function withErrorHandling(commandName, handler) {
return async (...paths) => {
try {
if (!paths.length) {
throw new Error(`The "${commandName}" command needs a path argument`);
}
await handler(...paths);
} catch (error) {
maxAPI.post('Error: ' + error.message);
maxAPI.outlet(FAILURE);
}
};
}

/**
* Expand paths that start with `~` to use the full home directory root.
* @param {string} path Path to expand.
* @returns {string} The full path.
*/
function expandTilde(path) {
if (!path.startsWith('~')) return path;
return os.homedir() + path.substr(1);
}

/**
* Resolve a path by expanding `~` and using Node’s native resolution method.
* @param {string} path Path to normalize.
* @returns {string} A normalized path.
*/
function normalizePath(path) {
return resolve(expandTilde(path));
}

/**
* Test if a file or directory exists at the provided path.
* @param {string} path Path to check existence of.
* @returns {Promise<void>}
*/
async function exists(path) {
const exists = existsSync(normalizePath(path));
maxAPI.outlet(exists ? SUCCESS : FAILURE);
}

/**
* Create a new directory at the provided path location.
* @param {string} path Path of directory to create.
* @returns {Promise<void>}
*/
async function makeDirectory(path) {
await mkdir(normalizePath(path));
maxAPI.outlet(SUCCESS);
}

/**
* Copy a file from one location to another.
* @param {string} source Path of file to copy.
* @param {string} destination Path to copy file to.
* @returns {Promise<void>}
*/
async function cp(source, destination) {
await copyFile(normalizePath(source), normalizePath(destination));
maxAPI.outlet(SUCCESS);
}
4 changes: 2 additions & 2 deletions package-info.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
"extensible": 0,
"homepatcher": "264 Tools Overview.maxpat",
"max_version_max": "none",
"max_version_min": "6.1.9",
"max_version_min": "8.0.0",
"name": "264 Tools",
"repository": "mus264/264-tools",
"os": {
Expand All @@ -16,7 +16,7 @@
"min_version": "none"
},
"windows": {
"platform": ["ia32"]
"platform": ["ia32", "x64"]
}

},
Expand Down
Loading

0 comments on commit 5a7d747

Please sign in to comment.