Przeglądaj źródła

Headless

tags/kitchen
Pete Shadbolt 3 lat temu
rodzic
commit
2d3d19084e
2 zmienionych plików z 128 dodań i 0 usunięć
  1. +69
    -0
      dev.scd
  2. +59
    -0
      headless.py

+ 69
- 0
dev.scd Wyświetl plik

@@ -0,0 +1,69 @@
// SVNVimStart

s = Server.local;
s.waitForBoot {
var module, msg, modules;

// Connect to OSC
thisProcess.openUDPPort(5005);
n = NetAddr.new("0.0.0.0", 5005);
o = OSCFunc({ arg msg, time, addr, recvPort; [msg, time, addr, recvPort].postln; }, '/radio', n);

// Create the synth definition and load it
module = SynthDef.new(\module, {
arg hue, saturation, value, pan, gain, octave;
var oscillator, noise, filter, panner, mixer, frequency, qfactor, noise_level, osc_level, lagtime, output, dust;

// Dynamic time of the module
lagtime = 1 / (2**octave);

// Oscillator/filter frequency
frequency = 130 + (hue * 130) * (2 ** octave);
frequency = Lag.kr(frequency, lagtime);
// Filtered saw oscillator
oscillator = SawDPW.ar(frequency);
filter = DFM1.ar(oscillator, frequency*1.2, 0.4, 1.0, 0.0, 0.0006);

// Apply pan
panner = LinPan2.ar(filter, pan);

// Apply dynamics
output = panner * Lag.kr(HPF.kr(value, 1 / (lagtime), 100), lagtime);
//output = panner * BPF.kr(value, 1 / lagtime, 100);

// Crank everything down
output = output * 0.1;

Out.ar(0, output);
});
module.load(s);
s.sync;

// Create multiple sound generators
modules = Array.fill(12,
{
arg index;
var pan, octave;
pan = 0 - ((index % 4) - 1.5)/1.5;
octave = (2 - (index / 4).floor) * 2;
"Module %: Pan %, octave %\n".postf(index, pan.round(1e-1), octave.round(1e-1));
Synth.new(\module,
[\hue, 0.5, \saturation, 0.1, \value, 0.5, \pan, pan, \gain, 0.9, \octave, octave]
)
}
);


// Hook up OSC
f = { |msg, time, addr|
if(msg[0] == '/radio') {
if(msg[1]<modules.size){
modules[msg[1]].set(\hue, msg[2]);
modules[msg[1]].set(\saturation, msg[3]);
modules[msg[1]].set(\value, msg[4]);
}
}
};
thisProcess.addOSCRecvFunc(f);
};

+ 59
- 0
headless.py Wyświetl plik

@@ -0,0 +1,59 @@
import math
import time
import itertools as it
import cv2
import colorsys
from pythonosc import udp_client
import numpy as np

DENSITY = 4
RED = [0, 0, 255]
N_COLORS = 3
LAST_MESSAGE_TIME = 0


def analyze_block(frame, osc, index, sp, ep, send=False):
""" Analyze a block """
block = frame[sp[1]:ep[1], sp[0]:ep[0], 0:3]
average_color = np.average(block, (0, 1)) / 255
h, s, v = colorsys.rgb_to_hsv(*average_color)

# Configure the oscillator
if send:
osc.send_message(
"/radio",
[int(index), float(h),
float(s), float(v), .5])
return frame


def analyze(frame, osc):
""" Analyze the full frame """
global LAST_MESSAGE_TIME
height, width, d = frame.shape
n = DENSITY
m = math.ceil(n * (height / width))
dx = width / n
dy = height / m
send = False
if time.time() - LAST_MESSAGE_TIME > 0.1:
LAST_MESSAGE_TIME = time.time()
send = True
for index, (y, x) in enumerate(it.product(range(m), range(n))):
sp = (int(x * dx), int(y * dy))
ep = (int(x * dx + dx), int(y * dy + dy))
frame = analyze_block(frame, osc, index, sp, ep, send)
return frame


if __name__ == '__main__':
# camera = cv2.VideoCapture("/dev/video2")
camera = cv2.VideoCapture(0)
osc = udp_client.SimpleUDPClient("0.0.0.0", 5005)
print("Radio is running...")

while True:
ret, frame = camera.read()
frame = analyze(frame, osc)

camera.release()

Ładowanie…
Anuluj
Zapisz