Browse Source

Working

tags/kitchen
Pete Shadbolt 3 years ago
parent
commit
be7f6ade5b
2 changed files with 40 additions and 39 deletions
  1. +24
    -19
      audio.scd
  2. +16
    -20
      radio.py

+ 24
- 19
audio.scd View File

@@ -8,49 +8,54 @@ s.waitForBoot {
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", {
module = SynthDef.new(\module, {
arg hue, saturation, value, pan, gain;
var oscillator, noise, filter, panner, mixer, frequency, qfactor, noise_level, osc_level;

// Calculate some parameters
frequency = 200 + 800*hue;
frequency = 800 + 500*hue;
qfactor = (1 - saturation)**4;
osc_level = saturation;
noise_level = 1 - osc_level;

// Generate some sounds
noise = WhiteNoise.ar(1);
// noise = WhiteNoise.ar(1);
noise = Crackle.ar(1.9, 1.0);
filter = BPF.ar(noise, frequency, qfactor) * noise_level;
oscillator = SinOsc.ar(frequency) * osc_level;
mixer = Mix.ar([filter, oscillator]);
panner = LinPan2.ar(mixer, 2*pan - 1, gain);
panner = LinPan2.ar(mixer, 2*pan - 1, value * 0.2);
Out.ar(0, panner);
});
module.load(s);
s.sync;

modules = [];
// Create multiple sound generators
modules = [];
1.do({
arg index;
var synth;
synth = Synth.new("module", [\hue, 0.5, \saturation, 0.1, \value, 0.5, \pan, 0.5, \gain, 0.9]);
modules.add(synth);
});
modules = Array.fill(12,
{
arg index;
var pan;
pan = ((index % 4) - 1.5)/1.5;
Synth.new(\module,
[\hue, 0.5, \saturation, 0.1, \value, 0.5, pan, 0.5, \gain, 0.9]
)
}
);

"%".postf(modules[0]);
"%\n".postf(modules[0]);
"%\n".postf(modules[1]);

// Hook up OSC
f = { |msg, time, addr|
if(msg[0] == '/radio') {
"Got data: index: % hue: % sat: % value: % pan: %\n".postf(msg[1], msg[2], msg[3], msg[4], msg[5]);
modules[msg[1]].set(\hue, msg[2]);
modules[msg[1]].set(\saturation, msg[3]);
modules[msg[1]].set(\value, msg[4]);
modules[msg[1]].set(\pan, msg[5]);
//s.sync;
if(msg[1]<modules.size){
"Applying to module %\n\n".postf(msg[1]);
modules[msg[1]].set(\hue, msg[2]);
modules[msg[1]].set(\saturation, msg[3]);
modules[msg[1]].set(\value, msg[4]);
modules[msg[1]].set(\pan, msg[5]);
}
}
};
thisProcess.addOSCRecvFunc(f);


+ 16
- 20
radio.py View File

@@ -2,6 +2,7 @@ import math
import time
import itertools as it
import cv2
import colorsys
from pythonosc import udp_client
import numpy as np

@@ -19,33 +20,28 @@ def draw_rectangle(frame, sp, ep):
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))
sp2 = tuple(int(x) for x in (2 * np.array(sp) + np.array(ep)) / 3)
ep2 = tuple(int(x) for x in (np.array(sp) + 2 * np.array(ep)) / 3)
average_color = [int(x) for x in average_color]
frame = cv2.rectangle(frame, sp2, ep2, average_color, -1)
average_color = np.uint8([[average_color]])
h, s, v = cv2.cvtColor(average_color, cv2.COLOR_BGR2HSV)[0][0]
average_color = np.average(block, (0, 1)) / 255
h, s, v = colorsys.rgb_to_hsv(*average_color)

# Configure the oscillator
if send and index == 0:
print("Sending message")
# and index == 0:
if send:
print("Sending message", h, s, v)
osc.send_message(
"/radio",
[int(index),
float(h / 255),
float(1),
float(v / 255), .5])
# osc.send_message(
# "/radio",
# [int(index),
# float(h / 255),
# float(s / 255),
# float(v / 255), .5])
[int(index), float(h),
float(s), float(v), .5])

# Draw a blob of the average color
sp2 = tuple(int(x) for x in (2 * np.array(sp) + np.array(ep)) / 3)
ep2 = tuple(int(x) for x in (np.array(sp) + 2 * np.array(ep)) / 3)
frame = cv2.rectangle(frame, sp2, ep2,
[int(x * 255) for x in average_color], -1)

# Draw the image
for thickness, color in ((3, (0, 0, 0)), (1, (255, 255, 255))):
frame = cv2.putText(frame,
text=f"{h:03d} {s:03d} {v:03d}",
text=f"{h:.2f} {s:.2f} {v:.2f}",
org=(sp[0] + 10, sp[1] + 20),
fontScale=.5,
color=color,


Loading…
Cancel
Save