From 254a25c183a4e3e3f609b3f8c2eb23eedc1e9020 Mon Sep 17 00:00:00 2001 From: Pete Shadbolt Date: Sun, 9 May 2021 08:33:56 -0700 Subject: [PATCH] Gooder noise --- audio.scd | 5 +++-- dev.scd | 27 +++++++++++++++++++++++++++ fm.scd | 28 ++++++++++++++++++++++++++++ radio.py | 32 +++++++++++++++++++++++--------- 4 files changed, 81 insertions(+), 11 deletions(-) create mode 100644 dev.scd create mode 100644 fm.scd diff --git a/audio.scd b/audio.scd index ea06eb0..b9c11f1 100644 --- a/audio.scd +++ b/audio.scd @@ -6,7 +6,6 @@ s.waitForBoot { 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); - //OSCFunc.trace(true); // Create the synth definition and load it module = SynthDef.new("module", { @@ -34,13 +33,15 @@ s.waitForBoot { // Create multiple sound generators modules = []; - 3.do({ + 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); }); + "%".postf(modules[0]); + // Hook up OSC f = { |msg, time, addr| if(msg[0] == '/radio') { diff --git a/dev.scd b/dev.scd new file mode 100644 index 0000000..e4eb1fb --- /dev/null +++ b/dev.scd @@ -0,0 +1,27 @@ +s = Server.local; +s.waitForBoot { + var synth, module; + + // Create the synth definition and load it + module = SynthDef.new("module", { + var noise, crackle, mixer, oscillator, filtered_noise, tone, frequency, noise_level, qfactor, osc_level; + + tone = MouseX.kr(1, 0); + frequency = MouseY.kr(200, 2000); + qfactor = (tone)**4; + osc_level = 1 - tone; + noise_level = 1 - tone; + + oscillator = SinOsc.ar(frequency) * osc_level; + crackle = Crackle.ar(tone + 1, 0.5); + noise = WhiteNoise.ar(1); + filtered_noise = BPF.ar(noise, frequency, qfactor) * noise_level; + mixer = Mix.ar([crackle, filtered_noise]); + Out.ar(0, mixer); + }); + + module.load(s); + s.sync; + + synth = Synth.new("module"); +}; diff --git a/fm.scd b/fm.scd new file mode 100644 index 0000000..1d23cd5 --- /dev/null +++ b/fm.scd @@ -0,0 +1,28 @@ +s = Server.local; +s.waitForBoot { + var synth, module; + + // Create the synth definition and load it + module = SynthDef.new("module", { + var mod, car, modPartial=2.401, carPartial=1, index=3, freq=440, mul=0.05; + + mod = SinOsc.ar( + freq * modPartial, + 0, + 200 + ); + + car = SinOsc.ar( + (freq * carPartial) + mod, + 0, + mul + ); + + Out.ar(0, Disintegrator.ar(SinOsc.ar(440), 0.5)); + }); + + module.load(s); + s.sync; + + synth = Synth.new("module"); +}; diff --git a/radio.py b/radio.py index b769ca3..1360671 100644 --- a/radio.py +++ b/radio.py @@ -1,4 +1,5 @@ import math +import time import itertools as it import cv2 from pythonosc import udp_client @@ -7,6 +8,7 @@ import numpy as np DENSITY = 4 RED = [0, 0, 255] N_COLORS = 3 +LAST_MESSAGE_TIME = 0 def draw_rectangle(frame, sp, ep): @@ -14,7 +16,7 @@ def draw_rectangle(frame, sp, ep): return cv2.rectangle(frame, sp, ep, RED, 1) -def analyze_block(frame, osc, index, 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)) @@ -26,13 +28,20 @@ def analyze_block(frame, osc, index, sp, ep): h, s, v = cv2.cvtColor(average_color, cv2.COLOR_BGR2HSV)[0][0] # Configure the oscillator - osc.send_message( - "/radio", - [int(index), - float(h / 255), - float(s / 255), - float(v / 255), .5]) - + if send and index == 0: + print("Sending message") + 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]) # Draw the image for thickness, color in ((3, (0, 0, 0)), (1, (255, 255, 255))): frame = cv2.putText(frame, @@ -47,16 +56,21 @@ def analyze_block(frame, osc, index, sp, ep): 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, (x, y) in enumerate(it.product(range(n), range(m))): sp = (int(x * dx), int(y * dy)) ep = (int(x * dx + dx), int(y * dy + dy)) frame = draw_rectangle(frame, sp, ep) - frame = analyze_block(frame, osc, index, sp, ep) + frame = analyze_block(frame, osc, index, sp, ep, send) return frame