Skip to content

Commit

Permalink
Functional LabsModule
Browse files Browse the repository at this point in the history
  • Loading branch information
vlcoo authored Apr 5, 2022
1 parent 78e3c32 commit 4c2c1bc
Show file tree
Hide file tree
Showing 11 changed files with 260 additions and 71 deletions.
15 changes: 8 additions & 7 deletions Channel.pde
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
import processing.sound.*;
import java.util.Stack;


public class ChannelOsc {
HashMap<Integer, SoundObject> current_notes; // Pairs <note midi code, oscillator object>
Env[] circular_array_envs;
int curr_env_index = 0;
float curr_global_amp = 1.0; // channel volume (0.0 to 1.0)
float amp_multiplier = 1.0; // basically expression
float amp_multiplier = 1.0; // basically expression
float curr_global_bend = 0.0; // channel pitch bend (-curr_bend_range to curr_bend_range semitones)
float curr_global_pan = 0.0; // channel stereo panning (-1.0 to 1.0)
float curr_bend_range = 2.0; // channel pitch bend range +/- semitones... uh, sure.
Expand All @@ -20,7 +21,7 @@ public class ChannelOsc {
float pulse_width = 0.5;
ChannelDisplay disp;

final int CIRCULAR_ARR_SIZE = 32;
final int CIRCULAR_ARR_SIZE = 16;

// values to be read by the display...:
float last_amp = 0.0;
Expand Down Expand Up @@ -65,18 +66,18 @@ public class ChannelOsc {
if (curr_global_amp <= 0 || silenced) return;
stop_note(note_code);

int mod_note_code = floor( note_code + curr_noteDetune + player.ktrans.transform[(note_code - 2 - player.mid_rootnote) % 12] );
int mod_note_code = floor( note_code + curr_noteDetune + player.ktrans.transform[(note_code - 2 + player.mid_rootnote) % 12] );
float freq = midi_to_freq(mod_note_code);
float amp = map(velocity, 0, 127, 0.0, 1.0);

Oscillator s = (Oscillator) current_notes.get(note_code);
if (s == null) {
s = get_new_osc(this.osc_type);
s.freq(freq + curr_freqDetune);
s.pan(curr_global_pan);
s.amp(amp * (osc_type == 1 || osc_type == 2 ? 0.12 : 0.05) * curr_global_amp * amp_multiplier); // give a volume boost to TRI and SIN
current_notes.put(note_code, s);
}
s.pan(curr_global_pan);
s.amp(amp * (osc_type == 1 || osc_type == 2 ? 0.12 : 0.05) * curr_global_amp * amp_multiplier); // give a volume boost to TRI and SIN

/*Env e = circular_array_envs[curr_env_index];
if (e == null) {
Expand All @@ -85,9 +86,9 @@ public class ChannelOsc {
}*/
if (osc_type == 0) ((Pulse) s).width(pulse_width);

if (env_values != null && env_values.length == 3) {
if (env_values != null && env_values.length == 4) {
Env e = new Env(PARENT);
e.play(s, env_values[0], env_values[1], 1.0, env_values[2]); // will come back to envelopes... great potential but buggy :(
e.play(s, env_values[0], env_values[1], env_values[2], env_values[3]); // will come back to envelopes... great potential but buggy :(
}
else s.play();

Expand Down
57 changes: 42 additions & 15 deletions LabsModule.pde
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ public class LabsModule extends PApplet {


public void settings() {
this.size(210, 220);
this.size(210, 290);
}


Expand All @@ -22,12 +22,12 @@ public class LabsModule extends PApplet {

this.selfFrame = ( (PSurfaceAWT.SmoothCanvas)this.surface.getNative() ).getFrame();
reposition();
redraw_all();
this.redraw_all();
}


public void draw() {

this.redraw_all();
}


Expand All @@ -40,7 +40,11 @@ public class LabsModule extends PApplet {
b3.show_label = false;
Button b4 = new Button("transform", "transform");
b4.show_label = false;
Button[] bs = new Button[] {b1, b2, b3, b4};
Button b5 = new Button("sysSynth", "sysSynth");
b5.show_label = false;
Button b6 = new Button("midiIn", "midiIn");
b6.show_label = false;
Button[] bs = new Button[] {b1, b2, b3, b4, b5, b6};
all_buttons = new ButtonToolbar(8, 45, 0, 1.3, bs);

}
Expand All @@ -58,7 +62,7 @@ public class LabsModule extends PApplet {
if (mouseButton == LEFT) {
if (all_buttons.collided("freqDetune", this)) {
try {
float val = Float.parseFloat(ui.showTextInputDialog("New detune in frequency?"));
float val = Float.parseFloat(ui.showTextInputDialog("New detune in Hz?"));
player.set_all_freqDetune(val);
}
catch (NumberFormatException nfe) {
Expand All @@ -69,7 +73,7 @@ public class LabsModule extends PApplet {

else if (all_buttons.collided("noteDetune", this)) {
try {
float val = Float.parseFloat(ui.showTextInputDialog("New detune in semitones?"));
float val = Float.parseFloat(ui.showTextInputDialog("New transpose in semitones?"));
player.set_all_noteDetune(val);
}
catch (NumberFormatException nfe) {
Expand All @@ -80,19 +84,19 @@ public class LabsModule extends PApplet {

else if (all_buttons.collided("tempo", this)) {
try {
int val = Integer.parseInt(ui.showTextInputDialog("New tempo in BPM?"));
if (val <= 0 || val > player.TEMPO_LIMIT) throw new NumberFormatException();
player.seq.setTempoInBPM(val);
float val = Float.parseFloat(ui.showTextInputDialog("New speed factor?"));
if (val <= 0 || val > 4) throw new NumberFormatException();
player.seq.setTempoFactor(val);
}
catch (NumberFormatException nfe) {
ui.showErrorDialog("Invalid value. Examples: 120, 90, 200", "Can't");
ui.showErrorDialog("Invalid value. Examples: 0.1, 0.5, 1, 2.8", "Can't");
}
catch (NullPointerException npe) {}
}

else if (all_buttons.collided("transform", this)) {
String selection = new UiBooster().showSelectionDialog(
"New mode?",
"New key/chord mode?",
"LabsModule",
new ArrayList(player.ktrans.available_transforms.keySet())
);
Expand All @@ -103,17 +107,38 @@ public class LabsModule extends PApplet {
curr_transform = selection;
}
}

else if (all_buttons.collided("sysSynth", this)) {
ui.showWarningDialog(
"Volume is louder and some options are not\n" +
"available while System Synth is ON.\n" +
"Please mind the loading time.",
"LabsModule"
);
PARENT.cursor(WAIT);
this.cursor(WAIT);
player.set_seq_synth(!player.system_synth);
PARENT.cursor(ARROW);
this.cursor(ARROW);
}

else if (all_buttons.collided("midiIn", this)) {
if (!player.midi_in_mode) player.start_midi_in();
else player.stop_midi_in();
}
}

this.redraw_all();
//this.redraw_all();
}


void mouseMoved() {
if (all_buttons.collided("freqDetune", this) ||
all_buttons.collided("noteDetune", this) ||
all_buttons.collided("tempo", this) ||
all_buttons.collided("transform", this)
all_buttons.collided("transform", this) ||
all_buttons.collided("sysSynth", this) ||
all_buttons.collided("midiIn", this)
) {
this.cursor(HAND);
}
Expand All @@ -136,13 +161,15 @@ public class LabsModule extends PApplet {
this.textFont(fonts[2]);
this.fill(t.theme[0]);
this.textAlign(CENTER, CENTER);
this.text("Welcome to P3synth's\nexperimenting module!", this.width/2, 22);
this.text("Experimental options!\nUse at own risk.", this.width/2, 22);

this.textFont(fonts[1]);
this.text(player.last_freqDetune, 179, 60);
this.text(player.last_noteDetune, 179, 99);
this.text(floor(player.seq.getTempoInBPM()), 179, 138);
this.text("x" + player.seq.getTempoFactor(), 179, 138);
this.text(curr_transform, 179, 177);
this.text((player.system_synth ? "On" : "Off"), 179, 216);
this.text((player.midi_in_mode ? "On" : "Off"), 179, 255);

all_buttons.redraw(this);
}
Expand Down
82 changes: 57 additions & 25 deletions MIDInServer.py
Original file line number Diff line number Diff line change
@@ -1,27 +1,59 @@
import socket
import socket, time
import mido
import mido.backends.rtmidi

HOST = "localhost"
PORT = 7726
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)

s.bind((HOST, PORT))
s.listen(10)
conn, addr = s.accept()

with mido.open_input(mido.get_input_names()[1]) as port:
while True:
msg = port.poll()
if msg is not None:
b = [str(x) for x in msg.bytes()]
try:
conn.send(f"{str(msg.channel)} {b[0]} {b[1]} {b[2]}\n".encode())
except (AttributeError, IndexError):
continue
'''
if midi_in.poll():
event = midi_in.read(1)
conn.send((str(event[0][0]).replace("[", "").replace("]", "") + "\n").encode())
'''

conn.close()
conn = None

def main():
global conn
print(" " + "_"*30 + "\n|_____P3synth_MIDIn_Server_____|")
print("Welcome! Warning: this is an unstable addon.\nFor detailed instructions, see the project's website.\n")

HOST = "localhost"
PORT = 7723

for n, i in enumerate(mido.get_input_names()):
print(f"{n} .. {i}")
port_num = mido.get_input_names()[int(input("Which port to listen to? "))]

s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)

print(f"\nSending to {HOST}:{PORT}...")
print("Please run P3synth and activate MIDI In mode!", end="\r")

s.bind((HOST, PORT))
s.listen(10)
conn, addr = s.accept()
print("OK!" + " "*50, end="\n\n")

with mido.open_input(port_num) as port:
while True:
msg = port.poll()
if msg is not None:
b = [str(x) for x in msg.bytes()]
print("[*]", end="\r")
try:
conn.send(f"{str(msg.channel)} {b[0]} {b[1]} {b[2]}\n".encode())
except (AttributeError, IndexError):
continue
except (BrokenPipeError, ConnectionResetError):
print("\nDisconnected!")
return

else:
time.sleep(0.01)
print("[ ]", end="\r")


if __name__ == "__main__":
try:
main()
if conn is not None:
conn.close()

except KeyboardInterrupt:
if conn is not None:
conn.send("goodbye".encode())
conn.close()

print("\nConnection closed.")
19 changes: 11 additions & 8 deletions P3synth.pde
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,8 @@ import java.awt.*;
import processing.awt.PSurfaceAWT;

final processing.core.PApplet PARENT = this;
final float VERCODE = 22.81;
final float VERCODE = 22.89;
final float OVERALL_VOL = 0.7;

Frame frame;
Player player;
Expand All @@ -29,7 +30,7 @@ void settings() {


void setup() {
new Sound(PARENT).volume(0.7); // oscillators at volume 1 are ridiculously loud...
new Sound(PARENT).volume(OVERALL_VOL); // oscillators at volume 1 are ridiculously loud...

SinOsc warmup = new SinOsc(PARENT);
warmup.freq(100);
Expand Down Expand Up @@ -59,7 +60,7 @@ void draw() {
player.redraw();
if (player.playing_state != -1) {
int n = (int) (player.seq.getTickPosition() / (player.midi_resolution/4)) % 8;
image(logo_anim[n], 311, 10);
image(logo_anim[abs(n)], 311, 10);
}
else {
if (player.vu_anim_val >= 0.0) {
Expand All @@ -83,7 +84,7 @@ void load_config(boolean just_opened) {
catch (FileNotFoundException fnfe) {
println("load fnfe");
ui.showWarningDialog(
"Welcome! You may want to lower the volume.\n\n" +
"Welcome! You may want to adjust your device's volume now.\n\n" +
"Press PLAY to begin or EXIT to quit at any time.",

"First time warning"
Expand Down Expand Up @@ -131,6 +132,8 @@ void redraw_all() {
b_meta_msgs.redraw();
b_reload_file.redraw();
b_labs.redraw();

if (player.syn != null) text(player.channels[0].curr_bend_range, 100, 20);
}


Expand Down Expand Up @@ -208,6 +211,8 @@ void mouseClicked() {
media_buttons.get_button("Exit").set_pressed(true);
//ui.showWaitingDialog("Exiting...", "Please wait");
player.set_playing_state(-1);
if (player.midi_in_mode) player.stop_midi_in();
player.seq.close();
exit();
}

Expand Down Expand Up @@ -240,7 +245,8 @@ void mouseClicked() {
"PAUSE: pause any playing music or resume if paused.\n" +
"EXIT: safely close the program.\n\n" +

"The buttons on the other side can control various options.\n\n" +
"The Labs menu has experimental playback/tinkering options!\n" +
"The buttons on the other side provide some info and configs.\n\n" +

"Press the X on any channel to mute it.\n" +
"You can use the lower left rectangle to control the song's position.\n" +
Expand Down Expand Up @@ -307,9 +313,6 @@ void mouseClicked() {
}
b_labs.set_pressed(!b_labs.pressed);
win_labs.reposition();

/*
*/
}

else {
Expand Down
Loading

0 comments on commit 4c2c1bc

Please sign in to comment.