Skip to content

Commit

Permalink
Merge pull request #26 from JvPeek/develop
Browse files Browse the repository at this point in the history
RTTTL + paw stay time depends on note length
  • Loading branch information
awsdcrafting authored Feb 3, 2024
2 parents 4e8f091 + 7ec8bcf commit 90ced4e
Show file tree
Hide file tree
Showing 6 changed files with 353 additions and 14 deletions.
5 changes: 3 additions & 2 deletions discord_bot/bot.py
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,7 @@ def parse_song(message: discord.Message):
return None

note_begin = message.content.find("!bongo")
title = message.content[:note_begin].strip()
title = message.content[:note_begin].strip().replace("\n", "\\n")
note_begin = message.content.find(" ", note_begin+1)
notes = message.content[note_begin:].strip()

Expand All @@ -121,7 +121,6 @@ def parse_song(message: discord.Message):
if "v" in notes:
notation = "bongo+"

pass
return Song(title, title.replace(" ", "_").lower(), message.author.name, notation, notes)


Expand Down Expand Up @@ -202,6 +201,8 @@ async def save_song(interaction: Interaction, message: Message):
if not song:
await interaction.response.send_message("No song detected!", ephemeral=True)
return
print(song)
print(message)
interaction.extras["song"] = song
interaction.extras["modal"] = SaveSongModal(default_song_title=song.title, default_file_name=song.file_name, default_author=song.author, default_notation=song.notation, default_notes=song.notes, message=message)
await interaction.response.send_modal(interaction.extras["modal"])
Expand Down
3 changes: 3 additions & 0 deletions src/bongocat.css
Original file line number Diff line number Diff line change
Expand Up @@ -126,4 +126,7 @@ body,html {
}
#instruments #TR808 {
background: url("images/808.png");
}
#instruments #nokia3210 {
background: url("images/NOKIA3210.png");
}
95 changes: 87 additions & 8 deletions src/bongocat.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import {parseSong as parseSongBongo} from "./modules/bongo.js";
import {experimentalFeatures} from "./experimental/bongox.js";

// ====================================================== //
// ================== type definitions ================== //
Expand Down Expand Up @@ -42,7 +43,7 @@ var queue = [];
var bongoEnabled = true;
var playing = false;
setBPM(128);
var githubUrls = ["https://raw.githubusercontent.com/jvpeek/twitch-bongocat/master/songs/","https://raw.githubusercontent.com/awsdcrafting/bongocat-songs/live/songs/"];
var githubUrls = ["https://raw.githubusercontent.com/jvpeek/twitch-bongocat/master/songs/", "https://raw.githubusercontent.com/awsdcrafting/bongocat-songs/live/songs/"];
var stackMode = false;
var maxSongLength = 90_000; //90 secs
var defaultNotation = "bongo";
Expand All @@ -52,6 +53,12 @@ var volume = 1.0;

window.maxNotesPerBatch = 5;

var audioContext;
var mainGainNode;
var oscillatorNode;
var synthStarted = false;
var synthDampening = 0.75;

// ====================================================== //
// ================== notation handlers ================= //
// ====================================================== //
Expand All @@ -69,6 +76,7 @@ notations["bongo+"] = parseSongBongo;
function setVolume(volumeParam)
{
volume = Math.min(1.0, Math.max(0, Number(volumeParam)));
mainGainNode.gain.value = volume * synthDampening;
}

function setMaxSongLength(maxSongLengthParam)
Expand All @@ -83,6 +91,20 @@ function setMaxSongLength(maxSongLengthParam)
}
}

function clamp(value, min, max)
{
if (value > max)
{
return max;
}
if (value < min)
{
return min;
}
return value;
//return Math.min(Math.max(value, min), max)
}

function setBPM(targetBPM, username)
{
targetBPM = Number(targetBPM);
Expand Down Expand Up @@ -139,6 +161,36 @@ function playSound(cmd, cBpm)
audio.play();
}

function prepareSynth(type)
{
audioContext = new AudioContext();
mainGainNode = audioContext.createGain();
mainGainNode.connect(audioContext.destination);
mainGainNode.gain.value = 0;
oscillatorNode = audioContext.createOscillator();
oscillatorNode.connect(mainGainNode);
oscillatorNode.type = type;
synthStarted = false;
return {"ctx": audioContext, "gain": mainGainNode, "osc": oscillatorNode};
}

function playSynthSound(frequency, time)
{
if (!synthStarted)
{
oscillatorNode.start();
oscillatorNode.stop(maxSongLength / 1000);
synthStarted = true;
}
mainGainNode.gain.setValueAtTime(volume * synthDampening, time);
oscillatorNode.frequency.setValueAtTime(frequency, time);
}

function muteSynth(time)
{
mainGainNode.gain.setValueAtTime(0, time);
}

function introAnimation(song)
{
let username = song.performer;
Expand Down Expand Up @@ -174,6 +226,15 @@ function outroAnimation()
{
document.getElementById("bongocat").style.left = "-1920px";
setInstrument("none");
if (mainGainNode)
{
mainGainNode.gain.value = 0;
}
if (oscillatorNode)
{
oscillatorNode.stop();
synthStarted = false;
}
setTimeout(function ()
{
playing = false;
Expand All @@ -198,7 +259,7 @@ function setPaw(paw, cBpm)
{
var currentPaw = document.getElementById(paw);
currentPaw.style.backgroundPosition = "top right";
window.setTimeout(releasePaw, cBpm / 2, paw);
window.setTimeout(releasePaw, 1000 * (60 / cBpm / 2), paw);
}

function releasePaw(paw)
Expand All @@ -213,7 +274,7 @@ function preparePlaybackObject(cmd, time, ...args)
return {time: time, cmd: cmd, args: args};
}

var helperMethods = {setBPM, getBPM, playSound, introAnimation, outroAnimation, setInstrument, setPaw, releasePaw, preparePlaybackObject};
var helperMethods = {clamp, setBPM, getBPM, playSound, prepareSynth, playSynthSound, muteSynth, introAnimation, outroAnimation, setInstrument, setPaw, releasePaw, preparePlaybackObject};
for (const key in helperMethods)
{
window[key] = helperMethods[key];
Expand Down Expand Up @@ -294,12 +355,16 @@ function checkQueue()
{
var song = getFromQueue();
let handler = notations[song.notation];
if (song.experimental)
{
handler = experimentalFeatures[song.notation] || handler;
}
if (handler)
{
currentSong = song;
currentSong.timeoutIDs = [];
introAnimation(song);
let playbacks = handler(song);
introAnimation(song);
console.log(playbacks);
for (let playback of playbacks)
{
Expand Down Expand Up @@ -329,21 +394,22 @@ async function playFromGithub(song, user)
song += ".json";

console.log("Playing", song, "from github for", user);
for (let githubUrl of githubUrls) {
for (let githubUrl of githubUrls)
{
const response = await fetch(encodeURI(githubUrl + song.trim()));
if (response.status != 200)
{
continue;
}
//console.log(response)
//console.log(response)
const jsonData = await response.json();
console.log(jsonData);
jsonData.performer = user;
jsonData.dedications = dedications;
addToQueue(jsonData);
break
break;
}


}

Expand Down Expand Up @@ -482,6 +548,19 @@ function bongo(args)
}
commands["!bongol"] = bongo;

function bongox(args)
{
let split = args.arg.indexOf(" ");
let experiment = args.arg.slice(0, split);
let notes = args.arg.slice(split + 1);
let username = args.tags.username;
let song = {performer: args.tags.username, notes: notes, notation: experiment, "experimental": true};
addToQueue(song);
//experimentalFeatures[experiment]?.(notes, username)
}
commands["!bongox"] = bongox;


function changeBpm(args)
{
if (!bongoEnabled)
Expand Down
Loading

0 comments on commit 90ced4e

Please sign in to comment.