Skip to content

Commit

Permalink
✨ Add support for the Track View class
Browse files Browse the repository at this point in the history
  • Loading branch information
leolabs committed Oct 24, 2023
1 parent f06d3d0 commit 3ff5980
Show file tree
Hide file tree
Showing 7 changed files with 125 additions and 21 deletions.
3 changes: 3 additions & 0 deletions midi-script/AbletonJS.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
from __future__ import absolute_import
import time


from .version import version
from .Config import DEBUG, FAST_POLLING
from .Logging import logger
Expand All @@ -18,6 +19,7 @@
from .Song import Song
from .SongView import SongView
from .Track import Track
from .TrackView import TrackView
from .Internal import Internal
from .ClipSlot import ClipSlot
from .Clip import Clip
Expand Down Expand Up @@ -52,6 +54,7 @@ def __init__(self, c_instance):
"song": Song(c_instance, self.socket),
"song-view": SongView(c_instance, self.socket),
"track": Track(c_instance, self.socket),
"track-view": TrackView(c_instance, self.socket),
"clip_slot": ClipSlot(c_instance, self.socket),
"clip": Clip(c_instance, self.socket),
}
Expand Down
11 changes: 0 additions & 11 deletions midi-script/Song.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,18 +4,12 @@
from .Device import Device
from .Scene import Scene
from .Track import Track
import Live

INSERT_MODES = {'default': Live.Track.DeviceInsertMode.default,
'left': Live.Track.DeviceInsertMode.selected_left,
'right': Live.Track.DeviceInsertMode.selected_right}


class Song(Interface):
def __init__(self, c_instance, socket):
super(Song, self).__init__(c_instance, socket)
self.song = self.ableton.song()
self._insert_mode = INSERT_MODES['default']

def get_ns(self, nsid):
return self.song
Expand Down Expand Up @@ -67,11 +61,6 @@ def get_current_smpte_song_time(self, ns, timeFormat):
def set_appointed_device(self, ns, device_id):
ns.appointed_device = Interface.get_obj(device_id)

def set_insert_mode(self, ns, args):
self._insert_mode = INSERT_MODES.get(
str(args), INSERT_MODES['default'])
self.song.view.selected_track.view.device_insert_mode = self._insert_mode

def safe_stop_playing(self, ns):
if self.song.is_playing:
self.song.stop_playing()
Expand Down
24 changes: 24 additions & 0 deletions midi-script/TrackView.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
from __future__ import absolute_import
from .Interface import Interface
from .Device import Device

import Live

INSERT_MODES = {'default': Live.Track.DeviceInsertMode.default,
'left': Live.Track.DeviceInsertMode.selected_left,
'right': Live.Track.DeviceInsertMode.selected_right}


class TrackView(Interface):
def __init__(self, c_instance, socket):
super(TrackView, self).__init__(c_instance, socket)

def get_ns(self, nsid):
return Interface.obj_ids[nsid].view

def get_selected_device(self, ns):
return Device.serialize_device(ns.selected_device)

def set_device_insert_mode(self, ns, name):
mode = INSERT_MODES.get(str(name), INSERT_MODES['default'])
ns.device_insert_mode = mode
10 changes: 0 additions & 10 deletions src/ns/song.ts
Original file line number Diff line number Diff line change
Expand Up @@ -66,12 +66,6 @@ export interface TransformedProperties {
scenes: Scene[];
}

export enum DeviceInsertMode {
default = "default",
left = "left",
right = "right",
}

export interface SettableProperties {
appointed_device: string;
arrangement_overdub: boolean;
Expand All @@ -82,7 +76,6 @@ export interface SettableProperties {
exclusive_arm: number;
exclusive_solo: number;
groove_amount: number;
insert_mode: DeviceInsertMode;
is_counting_in: boolean;
is_playing: boolean;
last_event_time: number;
Expand Down Expand Up @@ -346,7 +339,4 @@ export class Song extends Namespace<
public async undo() {
return this.sendCommand("undo");
}
public async set_insert_mode(args: DeviceInsertMode) {
return this.sendCommand("set_insert_mode", { args });
}
}
34 changes: 34 additions & 0 deletions src/ns/track-view.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
import { describe, it } from "vitest";
import { withAbleton } from "../util/tests";
import { DeviceInsertMode, GettableProperties } from "./track-view";

const gettableProps: (keyof GettableProperties)[] = [
"is_collapsed",
"selected_device",
];

describe("Track View", () => {
it("should be able to read all properties without erroring", async () => {
await withAbleton(async (ab) => {
const tracks = await ab.song.get("tracks");
const res = await Promise.all(
gettableProps.map((p) => tracks[0].view.get(p)),
);
console.log(res);
});
});

it("should be able to set the device insert mode", async () => {
await withAbleton(async (ab) => {
const tracks = await ab.song.get("tracks");
await tracks[0].view.set("device_insert_mode", DeviceInsertMode.Left);
});
});

it("should select the instrument device", async () => {
await withAbleton(async (ab) => {
const tracks = await ab.song.get("tracks");
await tracks[0].view.selectInstrument();
});
});
});
60 changes: 60 additions & 0 deletions src/ns/track-view.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
import { Ableton } from "..";
import { Namespace } from ".";
import { Device, RawDevice } from "./device";
import { Track, RawTrack } from "./track";
import { Scene, RawScene } from "./scene";
import { RawDeviceParameter, DeviceParameter } from "./device-parameter";
import { ClipSlot, RawClipSlot } from "./clip-slot";

export enum DeviceInsertMode {
Default = "default",
Left = "left",
Right = "right",
}

export interface GettableProperties {
// device_insert_mode: DeviceInsertMode; – for some reason, Live returns a boolean here
is_collapsed: boolean;
selected_device: RawDevice;
}

export interface TransformedProperties {
selected_device: Device;
}

export interface SettableProperties {
device_insert_mode: DeviceInsertMode;
is_collapsed: boolean;
}

export interface ObservableProperties {
// device_insert_mode: DeviceInsertMode;
is_collapsed: boolean;
selected_device: RawDevice;
}

export class TrackView extends Namespace<
GettableProperties,
TransformedProperties,
SettableProperties,
ObservableProperties
> {
constructor(ableton: Ableton, nsid: string) {
super(ableton, "track-view", nsid);

this.transformers = {
selected_device: (device) => new Device(ableton, device),
};

this.cachedProps = {
selected_device: true,
};
}

/**
* Selects the track's instrument if it has one.
*/
async selectInstrument() {
return this.sendCommand("select_instrument");
}
}
4 changes: 4 additions & 0 deletions src/ns/track.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import { ClipSlot, RawClipSlot } from "./clip-slot";
import { MixerDevice, RawMixerDevice } from "./mixer-device";
import { Clip, RawClip } from "./clip";
import { Color } from "../util/color";
import { TrackView } from "./track-view";

export enum RoutingLayout {
Mono = 1,
Expand Down Expand Up @@ -185,8 +186,11 @@ export class Track extends Namespace<
SettableProperties,
ObservableProperties
> {
view: TrackView;

constructor(ableton: Ableton, public raw: RawTrack) {
super(ableton, "track", raw.id);
this.view = new TrackView(this.ableton, raw.id);

this.transformers = {
arrangement_clips: (clips: RawClip[]) =>
Expand Down

0 comments on commit 3ff5980

Please sign in to comment.