Skip to content

Commit

Permalink
Fix and check classes, tests and examples
Browse files Browse the repository at this point in the history
  • Loading branch information
crucialfelix committed Nov 27, 2019
1 parent 46b17b2 commit 475916b
Show file tree
Hide file tree
Showing 16 changed files with 124 additions and 117 deletions.
15 changes: 8 additions & 7 deletions examples/dryads-synth-event-list.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,27 +13,28 @@
*/
const { play, SCSynthDef, SynthEventList, SCServer } = require("supercolliderjs").dryads;

function randomEvent() {
function randomEvent(totalDuration) {
return {
time: Math.round(Math.random() * 20) * 0.25,
time: Math.random() * totalDuration,
defName: "saw",
args: {
freq: Math.random() * 500 + 100,
},
};
}

function randomEvents(n = 256) {
function randomEvents(totalDuration = 60, density = 2) {
const n = Math.floor(totalDuration * density);
const events = [];
for (let index = 0; index < n; index++) {
events.push(randomEvent());
events.push(randomEvent(totalDuration));
}
return events;
}

// Currently this has to be expressed as a tree of nested dependencies.
// dryadic 2 will make it much cleaner and simpler.
const out = new SCServer({}, [
const out = new SCServer({ numInputBusChannels: 0 }, [
new SCSynthDef(
{
source: `
Expand All @@ -44,8 +45,8 @@ const out = new SCServer({}, [
},
[
new SynthEventList({
events: randomEvents,
loopTime: 4,
events: randomEvents(60, 4),
loopTime: 65,
}),
],
),
Expand Down
32 changes: 21 additions & 11 deletions examples/sine-wave.js
Original file line number Diff line number Diff line change
@@ -1,13 +1,23 @@
// const d = require("supercolliderjs").dryads;
const d = require("supercolliderjs").dryads;

// const s = new d.Synth(
// `
// arg freq;
// Out.ar(0, SinOsc.ar(freq))
// `,
// {
// freq: 40,
// },
// );
/**
* This is the full tree required just to play a Sin wave.
* Dryadic 2 will
*/
const out = new d.SCLang({}, [
new d.SCServer({ numInputBusChannels: 0 }, [
new d.Synth({
def: new d.SCSynthDef({
source: `
{ arg freq;
Out.ar(0, SinOsc.ar(freq))
}`,
}),
args: {
freq: 440,
},
}),
]),
]);

// d.dryadic(s).play();
d.dryadic(out).play();
8 changes: 4 additions & 4 deletions packages/dryads/src/AudioBus.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { Dryad } from "dryadic";
import { Dryad, Command, CallOrder } from "dryadic";
import Server from "@supercollider/server";

interface Properties {
Expand Down Expand Up @@ -30,17 +30,17 @@ export default class AudioBus extends Dryad<Properties> {
return "SCServer";
}

prepareForAdd(): object {
prepareForAdd(): Command {
return {
callOrder: "SELF_THEN_CHILDREN",
callOrder: CallOrder.SELF_THEN_CHILDREN,
updateContext: (context: Context, properties: Properties) => ({
out: context.scserver.state.allocAudioBus(properties.numChannels),
numChannels: properties.numChannels,
}),
};
}

remove(): object {
remove(): Command {
return {
run: (context: Context, properties: Properties) =>
context.scserver.state.freeAudioBus(context.out, properties.numChannels),
Expand Down
10 changes: 5 additions & 5 deletions packages/dryads/src/Group.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { Dryad } from "dryadic";
import { Dryad, Command, CallOrder } from "dryadic";

import Server, { msg, whenNodeEnd, whenNodeGo } from "@supercollider/server";

Expand All @@ -24,9 +24,9 @@ export default class Group extends Dryad {
return "SCServer";
}

prepareForAdd(): object {
prepareForAdd(): Command {
return {
callOrder: "SELF_THEN_CHILDREN",
callOrder: CallOrder.SELF_THEN_CHILDREN,
updateContext: (context: Context /*, properties*/) => {
const nodeID = context.scserver.state.nextNodeID();
return {
Expand All @@ -44,7 +44,7 @@ export default class Group extends Dryad {
};
}

add(): object {
add(): Command {
return {
scserver: {
msg: (context: Context) => groupNew(context.nodeID, AddActions.TAIL, context.parentGroup),
Expand All @@ -53,7 +53,7 @@ export default class Group extends Dryad {
};
}

remove(): object {
remove(): Command {
return {
scserver: {
// children do not have to free their nodes
Expand Down
8 changes: 4 additions & 4 deletions packages/dryads/src/SCLang.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { Dryad } from "dryadic";
import { Dryad, CallOrder, Command } from "dryadic";
import _ from "lodash";

import LanguageServer, { boot, SCLangArgs } from "@supercollider/lang";
Expand Down Expand Up @@ -37,16 +37,16 @@ export default class SCLang extends Dryad<Properties> {
};
}

prepareForAdd(): object {
prepareForAdd(): Command {
return {
callOrder: "SELF_THEN_CHILDREN",
callOrder: CallOrder.SELF_THEN_CHILDREN,
updateContext: (context: Context, properties: Properties) => ({
sclang: boot(_.defaults(properties.options, { log: context.log })),
}),
};
}

remove(): object {
remove(): Command {
return {
run: (context: Context) => {
return context.sclang && context.sclang.quit();
Expand Down
16 changes: 8 additions & 8 deletions packages/dryads/src/SCServer.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { Dryad } from "dryadic";
import { Dryad, CallOrder, Command, Properties } from "dryadic";
import _ from "lodash";

import Server, { boot, ServerArgs } from "@supercollider/server";
Expand All @@ -8,7 +8,7 @@ const defaultOptions = {
debug: false,
};

interface Properties {
interface ServerProperties extends Properties {
options: ServerArgs;
}

Expand All @@ -27,8 +27,8 @@ interface Context {
* `options` are the command line options supplied to scsynth (note: not all options are passed through yet)
* see {@link Server}
*/
export default class SCServer extends Dryad<Properties> {
defaultProperties(): Properties {
export default class SCServer extends Dryad<ServerProperties> {
defaultProperties(): ServerProperties {
return {
options: defaultOptions,
};
Expand All @@ -41,16 +41,16 @@ export default class SCServer extends Dryad<Properties> {
};
}

prepareForAdd(): object {
prepareForAdd(): Command {
return {
callOrder: "SELF_THEN_CHILDREN",
updateContext: (context: Context, properties: Properties) => ({
callOrder: CallOrder.SELF_THEN_CHILDREN,
updateContext: (context: Context, properties: ServerProperties) => ({
scserver: boot(_.defaults(properties.options, { log: context.log })),
}),
};
}

remove(): object {
remove(): Command {
return {
run: (context: Context) => {
if (context.scserver) {
Expand Down
20 changes: 9 additions & 11 deletions packages/dryads/src/SCSynthDef.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
/* eslint no-console: 0 */
import { Dryad } from "dryadic";
import SCLang, { SCLangError } from "@supercollider/lang";
import Server, { msg, MsgType } from "@supercollider/server";
import { CallOrder, Command, Dryad } from "dryadic";
import fs from "fs";
import path from "path";

import SCLang, { SCLangError } from "@supercollider/lang";
import Server, { msg, MsgType } from "@supercollider/server";
const { defFree, defLoad, defRecv } = msg;

const fsp = fs.promises;
Expand Down Expand Up @@ -46,16 +46,14 @@ interface Context {
scserver?: Server;
_watcher?: any;
}

/**
* Compile a SynthDef from sclang source code
* or load a precompiled .scsyndef
*
* If compilation is required then it will insert SCLang as a parent if necessary.
*
*
*
* Note that the synthDefName is not known until after the source code is compiled.
*
*/
export default class SCSynthDef extends Dryad<Properties> {
defaultProperties(): Properties {
Expand All @@ -71,13 +69,13 @@ export default class SCSynthDef extends Dryad<Properties> {
}
}

prepareForAdd(): object {
prepareForAdd(): Command {
// search context for a SynthDefCompiler, else create one with context.lang
return {
updateContext: (context: Context, properties: Properties) => ({
synthDef: this._prepareForAdd(context, properties),
}),
callOrder: "SELF_THEN_CHILDREN",
callOrder: CallOrder.SELF_THEN_CHILDREN,
};
}

Expand Down Expand Up @@ -182,7 +180,7 @@ export default class SCSynthDef extends Dryad<Properties> {
return this.compileSource(context, source, sourcePath);
}

add(): object {
add(): Command {
return {
run: (context: Context, properties: Properties) => {
if (properties.compileFrom && properties.watch) {
Expand All @@ -199,7 +197,7 @@ export default class SCSynthDef extends Dryad<Properties> {
};
}

remove(): object {
remove(): Command {
return {
scserver: {
// no need to do this if server has gone away
Expand All @@ -218,7 +216,7 @@ export default class SCSynthDef extends Dryad<Properties> {
};
}

putSynthDef(context: Context, synthDefName: string, synthDesc: object) {
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
8 changes: 4 additions & 4 deletions packages/dryads/src/Synth.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { Dryad } from "dryadic";
import { Dryad, Command } from "dryadic";
import _ from "lodash";

import Server, { OscType, updateNodeState, whenNodeEnd, whenNodeGo, msg } from "@supercollider/server";
Expand Down Expand Up @@ -38,7 +38,7 @@ export default class Synth extends Dryad<Properties> {
return "SCServer";
}

prepareForAdd(): object {
prepareForAdd(): Command {
return {
updateContext: context => ({
nodeID: context.scserver.state.nextNodeID(),
Expand All @@ -55,7 +55,7 @@ export default class Synth extends Dryad<Properties> {
// return name;
// }

add(): object {
add(): Command {
const defName = def => (typeof def === "string" ? def : def.name);
return {
scserver: {
Expand Down Expand Up @@ -84,7 +84,7 @@ export default class Synth extends Dryad<Properties> {
};
}

remove(): object {
remove(): Command {
return {
scserver: {
msg: (context: Context) => nodeFree(context.nodeID || -1),
Expand Down
6 changes: 3 additions & 3 deletions packages/dryads/src/SynthControl.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { msg, OscType } from "@supercollider/server";
import { EventStream } from "baconjs";
import { Dryad, DryadPlayer } from "dryadic";
import { Dryad, DryadPlayer, Command } from "dryadic";
import _ from "lodash";

const { nodeSet } = msg;
Expand Down Expand Up @@ -36,7 +36,7 @@ export default class SynthControl extends Dryad<Properties> {
return "SCServer";
}

add(player: DryadPlayer): object {
add(player: DryadPlayer): Command {
return {
run: (context: Context, properties: Properties) => {
if (properties.stream) {
Expand All @@ -59,7 +59,7 @@ export default class SynthControl extends Dryad<Properties> {
};
}

remove(): object {
remove(): Command {
return {
run: (context: Context) => {
if (context.subscription) {
Expand Down
Loading

0 comments on commit 475916b

Please sign in to comment.