Skip to content

Commit

Permalink
Changes to source, types and doc strings
Browse files Browse the repository at this point in the history
  • Loading branch information
crucialfelix committed Jan 9, 2020
1 parent 4531069 commit c298c74
Show file tree
Hide file tree
Showing 13 changed files with 172 additions and 197 deletions.
14 changes: 9 additions & 5 deletions packages/dryads/src/SCSynthDef.ts
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,7 @@ export default class SCSynthDef extends Dryad<Properties> {
};
}

async _prepareForAdd(context: Context, properties: Properties): Promise<CompiledSynthDef | LoadedSynthDef> {
private async _prepareForAdd(context: Context, properties: Properties): Promise<CompiledSynthDef | LoadedSynthDef> {
if (properties.source) {
const result = await this.compileSource(context, properties.source);
await this._sendSynthDef(context, properties, result);
Expand Down Expand Up @@ -124,7 +124,11 @@ export default class SCSynthDef extends Dryad<Properties> {
);
}

async _sendSynthDef(context: Context, properties: Properties, result: CompiledSynthDef): Promise<CompiledSynthDef> {
private async _sendSynthDef(
context: Context,
properties: Properties,
result: CompiledSynthDef,
): Promise<CompiledSynthDef> {
// ! alters context
// name bytes
// synthDefName should be set for child context
Expand All @@ -144,7 +148,7 @@ export default class SCSynthDef extends Dryad<Properties> {
return result;
}

async _writeSynthDef(name: string, buffer: Buffer, synthDesc: SynthDesc, saveToDir: string): Promise<void> {
private async _writeSynthDef(name: string, buffer: Buffer, synthDesc: SynthDesc, saveToDir: string): Promise<void> {
const dir = path.resolve(saveToDir);
const pathname = path.join(dir, name + ".scsyndef");
await fsp.writeFile(pathname, buffer);
Expand Down Expand Up @@ -189,7 +193,7 @@ export default class SCSynthDef extends Dryad<Properties> {
/**
* Returns a Promise for a SynthDef result object: name, bytes, synthDesc
*/
async compileFrom(context: Context, sourcePath: string): Promise<CompiledSynthDef> {
private async compileFrom(context: Context, sourcePath: string): Promise<CompiledSynthDef> {
// TODO: utf-8, no?
const source = (await fsp.readFile(path.resolve(sourcePath))).toString("ascii");
return this.compileSource(context, source, sourcePath);
Expand Down Expand Up @@ -231,7 +235,7 @@ export default class SCSynthDef extends Dryad<Properties> {
};
}

putSynthDef(context: Context, synthDefName: string, synthDesc: object): void {
private putSynthDef(context: Context, synthDefName: string, synthDesc: object): void {
context.scserver &&
context.scserver.state.mutate(StateKeys.SYNTH_DEFS, state => {
return state.set(synthDefName, synthDesc);
Expand Down
2 changes: 1 addition & 1 deletion packages/dryads/src/Synth.ts
Original file line number Diff line number Diff line change
Expand Up @@ -113,7 +113,7 @@ export default class Synth extends Dryad<Properties> {
};
}

_checkOscType(v: any, key: string, id: string): any {
private _checkOscType(v: any, key: string, id: string): any {
switch (typeof v) {
case "number":
case "string":
Expand Down
6 changes: 3 additions & 3 deletions packages/dryads/src/SynthEventList.ts
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ interface AddCommand extends Command {
/**
* Takes a list of synth event objects with relative times and schedules them.
*
* ## properties
* #### properties
*
* __events:__ Array
*
Expand Down Expand Up @@ -147,12 +147,12 @@ export default class SynthEventList extends Dryad<Properties> {
return commands;
}

_makeSchedLoop(events: SynthEvent[], loopTime: number | undefined, context: Context): Function {
private _makeSchedLoop(events: SynthEvent[], loopTime: number | undefined, context: Context): Function {
const synthEvents = this._makeMsgs(events, context);
return loopTime ? loopedEventListIterator(synthEvents, loopTime) : eventListIterator(synthEvents);
}

_makeMsgs(events: SynthEvent[], context: Context): OSCEvent[] {
private _makeMsgs(events: SynthEvent[], context: Context): OSCEvent[] {
const defaultParams = this.properties.defaultParams || {};
return events.map(event => {
// TODO: do this a jit time in the schedLoop
Expand Down
2 changes: 1 addition & 1 deletion packages/dryads/src/__tests__/SynthEventList.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ describe("SynthEventList", function() {
describe("_makeMsgs", function() {
// context group epoch
const sel = new SynthEventList();
const scheded = sel._makeMsgs(events, context);
const scheded = sel["_makeMsgs"](events, context);
const first = scheded[0];

it("should have events in the packet", function() {
Expand Down
20 changes: 15 additions & 5 deletions packages/dryads/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
/**
* @module dryads
* Dryadic components for SuperCollider
*
* @package dryads
*/
import scserver from "./middleware/scserver";

Expand All @@ -21,12 +23,18 @@ import { Dryad, DryadPlayer, Middleware, dryadic as makeDryadPlayer, Layer } fro
export { SCServer, SCLang, Group, Synth, AudioBus, SCSynthDef, SynthControl, SynthStream, SynthEventList };

const middleware: Middleware[] = [scserver];
const classes = [SCServer, SCLang, Group, Synth, AudioBus, SCSynthDef, SynthControl, SynthStream, SynthEventList];

/**
* A layer adds Dryad classes and middleware for an external library. eg. for Supercolliderjs
*/
export const layer: Layer = {
middleware,
classes: [SCServer, SCLang, Group, Synth, AudioBus, SCSynthDef, SynthControl, SynthStream, SynthEventList],
classes,
};

// export const ServerMiddleware = scserver;

export interface Context {
[name: string]: any;
}
Expand All @@ -37,7 +45,7 @@ export interface Context {
* Automatically includes the supercollider.js layer
*
* usage:
*
* ```js
* var sc = require('supercolliderjs');
* var player = sc.dryadic([
* 'scserver', [
Expand All @@ -51,8 +59,9 @@ export interface Context {
* ]
* ]);
* player.play();
* ...
* // ...
* player.stop();
* ```
*/
export function dryadic(rootDryad?: Dryad, moreLayers: Layer[] = [], rootContext: Context = {}): DryadPlayer {
return makeDryadPlayer(rootDryad || new Dryad(), [layer, ...moreLayers], rootContext);
Expand All @@ -63,6 +72,7 @@ export function dryadic(rootDryad?: Dryad, moreLayers: Layer[] = [], rootContext
*
* usage:
*
* ```js
* var sc = require('supercolliderjs');
* var player = sc.play([
* 'scserver', [
Expand All @@ -75,7 +85,7 @@ export function dryadic(rootDryad?: Dryad, moreLayers: Layer[] = [], rootContext
* }]
* ]
* ]);
*
* ```
* @param {Dryad|Array} rootDryad - Dryad object or hyperscript document
* @returns {DryadPlayer}
*/
Expand Down
9 changes: 1 addition & 8 deletions packages/dryads/src/middleware/scserver.ts
Original file line number Diff line number Diff line change
Expand Up @@ -84,14 +84,8 @@ interface ServerCommand extends Command {
* and only sending them to the server just before they should play. This doesn't overload
* the server with a glut of messages and also allows cancellation and updating of the messages
* and makes it easy to implement transport controls and looping.
*
*
* @param {object} context
* @param {object} properties
* @return Promise is only returned when using .callAndResponse
*/

const scserver: Middleware = async (
export const scserver: Middleware = async (
command: ServerCommand,
context: Context,
properties: Properties,
Expand Down Expand Up @@ -128,7 +122,6 @@ const scserver: Middleware = async (
}

// Preparation commands that get an OSC callback from the server.
// Only this one returns.
// These are used only in preparation, not for play / update.
if (cmds.callAndResponse) {
await ctx.scserver.callAndResponse(cmds.callAndResponse);
Expand Down
21 changes: 13 additions & 8 deletions packages/scapi/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@ interface RequestHandlers {
}

/*
*
* Communicates via OSC with the SuperCollider API quark
*
* The 'API' quark implements a two-way communication protocol.
Expand All @@ -36,11 +35,11 @@ interface RequestHandlers {
* See examples/call-api-from-node.js
*/
export default class SCAPI extends events.EventEmitter {
schost: string;
scport: number;
requests: RequestHandlers = {};
log: Logger;
udp?: dgram.Socket;
readonly schost: string;
readonly scport: number;
private requests: RequestHandlers = {};
private log: Logger;
private udp?: dgram.Socket;

constructor(schost = "localhost", scport = 57120) {
super();
Expand Down Expand Up @@ -74,7 +73,13 @@ export default class SCAPI extends events.EventEmitter {
}
}

call(requestId, oscpath, args, ok, err) {
call(
requestId: string | undefined,
oscpath: string,
args,
ok?: (value: unknown) => any,
err?: ((reason: any) => any) | null | undefined,
): Promise<any> {
const promise = new Promise((resolve, reject) => {
const clientId = 0; // no longer needed
let clumps: RegExpMatchArray | null = null;
Expand Down Expand Up @@ -132,7 +137,7 @@ export default class SCAPI extends events.EventEmitter {
}
}

receive(signal, msg): void {
private receive(signal, msg): void {
const requestId = msg.args[1];
let result = msg.args[2];
const request = this.requests[requestId];
Expand Down
35 changes: 18 additions & 17 deletions packages/server-plus/src/ServerPlus.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ type Params = msg.Params;
*
* See `server.group(...)`
*/
class Group {
export class Group {
id: number;
server: ServerPlus;

Expand Down Expand Up @@ -65,11 +65,11 @@ class Group {
}

/**
* scsynth Synth
* Created with `server.synth(...)`
*
* See `server.synth(...)`
* Extends Group
*/
class Synth extends Group {
export class Synth extends Group {
// store def and args
// synthDef
// release
Expand All @@ -78,13 +78,13 @@ class Synth extends Group {

/**
* scsynth audio bus
*
* See `server.audioBus(...)`
*
* These bus numbers (ids) and numChannels are allocated here in the client.
* The server only gets bus ids for reading and writing to.
*/
class AudioBus {
export class AudioBus {
id: number;
server: ServerPlus;
numChannels: number;
Expand All @@ -104,14 +104,14 @@ class AudioBus {
}

/**
* scsynth control bus
* See `server.controlBus(...)`
*
* These bus numbers (ids) and numChannels are allocated here in the client.
* The server only gets bus ids for reading and writing to.
*/
class ControlBus extends AudioBus {
* scsynth control bus
* See `server.controlBus(...)`
*
* These bus numbers (ids) and numChannels are allocated here in the client.
* The server only gets bus ids for reading and writing to.
*/
export class ControlBus extends AudioBus {
/**
* Deallocate the ControlBus, freeing it for resuse.
*/
Expand All @@ -128,7 +128,7 @@ class ControlBus extends AudioBus {
*
* See `server.buffer(...)` and `server.readBuffer(...)`
*/
class Buffer {
export class Buffer {
id: number;
server: ServerPlus;
numFrames: number;
Expand Down Expand Up @@ -172,7 +172,7 @@ class Buffer {
* The SynthDef may have been compiled from a sourceCode string
* or compiled from a file at path.
*/
class SynthDef {
export class SynthDef {
server: ServerPlus;
name: string;
synthDefResult: SynthDefResultType;
Expand Down Expand Up @@ -231,7 +231,8 @@ export default class ServerPlus extends Server {
): Promise<Synth> {
const [def, g] = await Promise.all([Promise.resolve(synthDef), Promise.resolve(group)]);
const nodeId = this.state.nextNodeID();
const sn = msg.synthNew(def.synthDefResult.name, nodeId, addAction, g ? g.id : 0, args);
// src/ServerPlus.ts:236:29 - error TS2532: Object is possibly 'undefined'. ?
const sn = msg.synthNew((def as SynthDef).synthDefResult.name, nodeId, addAction, g ? g.id : 0, args);
this.send.msg(sn);
await whenNodeGo(this, String(nodeId), nodeId);
return new Synth(this, nodeId);
Expand Down
Loading

0 comments on commit c298c74

Please sign in to comment.