Quick actions

cmd+k|ctrl+k

Navigation

Languages

music system overhaul

Snippet info

Language

Lua

Visibility

public

Author

dinoturto

Created

2023-09-22T03:25:13.65733Z

Updated

2023-09-25T18:17:05.079741Z

local random = Random.new()
local soundTable = {
    ["Bass"] = {};
    ["Ping"] = {};
    ["Snap"] = {}
}

local letterTable = {
    ["C"] = 2 ^ (1 / 12);
    ["C#"] = 2 ^ (2 / 12);
    ["D"] = 2 ^ (3 / 12);
    ["D#"] = 2 ^ (4 / 12);
    ["E"] = 2 ^ (5 / 12);
    ["F"] = 2 ^ (6 / 12);
    ["F#"] = 2 ^ (7 / 12);
    ["G"] = 2 ^ (8 / 12);
    ["G#"] = 2 ^ (9 / 12);
    ["A"] = 2 ^ (10 / 12);
    ["A#"] = 2 ^ (11 / 12);
    ["B"] = 2 ^ (12 / 12)
}

local octaveTable = {
    [1] = 0.125;
    [2] = 0.25;
    [3] = 0.5;
    [4] = 1;
    [5] = 2;
    [6] = 4;
    [7] = 8
}

local function returnPitch(table)
    local letter = table[1]
    local octave = table[2]
    return letterTable[letter] * octaveTable[octave]
end

local currentBass = 1
local currentPing = 1
local currentSnap = 1

for i = 1, 3 do
    for s = 1, 6 do
        local sEffect = Instance.new("Sound")
        sEffect.Volume = 1.4
        sEffect.Parent = owner.Character.HumanoidRootPart
        
        if i == 1 then
            sEffect.SoundId = "rbxasset://sounds/bass.mp3"
            table.insert(soundTable["Bass"], {sEffect, "C"})
        elseif i == 2 then
            sEffect.SoundId = "rbxasset://sounds/electronicpingshort.wav"
            table.insert(soundTable["Ping"], {sEffect, "A#"})
        elseif i == 3 then
            sEffect.SoundId = "rbxasset://sounds/snap.mp3"
            table.insert(soundTable["Snap"], {sEffect, "C"})
        end
    end
end

local function playSound(s, p, v)
    if s == "bass" then
        currentBass += 1
        if currentBass > #soundTable["Bass"] then
            currentBass = 1
        end
        
        local sound = soundTable["Bass"][currentBass][1]
        sound.TimePosition = 0.12
        sound.Pitch = returnPitch(p) * letterTable[soundTable["Bass"][currentBass][2]]
        sound.Volume = v
        sound.Playing = true
        return sound
    elseif s == "ping" then
        currentPing += 1
        if currentPing > #soundTable["Ping"] then
            currentPing = 1
        end
        
        local sound = soundTable["Ping"][currentPing][1]
        sound.TimePosition = 0.2
        sound.Pitch = returnPitch(p) * letterTable[soundTable["Ping"][currentPing][2]]
        sound.Volume = v
        sound.Playing = true
        return sound
    elseif s == "snap" then
        currentSnap += 1
        if currentSnap > #soundTable["Snap"] then
            currentSnap = 1
        end
        
        local sound = soundTable["Snap"][currentSnap][1]
        sound.TimePosition = 0.2
        sound.Pitch = returnPitch(p) * letterTable[soundTable["Snap"][currentSnap][2]]
        sound.Volume = v
        sound.Playing = true
        return sound
    end
end

local sounds = {
    ["NightSong"] = {
        ["Tempo"] = 135;
        ["Channels"] = {
            [1] = "bass";
            [2] = "ping"
        };
        ["Patterns"] = {
            [1] = {
                [1] = {
                    [1] = {"F#", 4, 0.2, 2, true};
                    [2] = {"F#", 4, 0.4, 2, true};
                    [3] = {"F#", 4, 0.6, 2, true};
                    [4] = {"F#", 4, 0.8, 2, true};
                    [5] = {"F#", 4, 1, 2, true};
                    [6] = {"F#", 4, 1.2, 2, true};
                    [7] = {"F#", 4, 1.4, 2, true};
                    [8] = {"F#", 4, 1.6, 2, true};
                    [9] = {"F#", 4, 1.6, 2, true};
                    [10] = {"F#", 4, 1.4, 2, true};
                    [11] = {"F#", 4, 1.2, 2, true};
                    [12] = {"F#", 4, 1, 2, true};
                    [13] = {"F#", 4, 0.8, 2, true};
                    [14] = {"F#", 4, 0.6, 2, true};
                    [15] = {"F#", 4, 0.4, 2, true};
                    [16] = {"F#", 4, 0.2, 2, true}
                };
                [2] = {
                    [1] = {"A#", 2, 2, 2, true};
                    [2] = {"C", 3, 2, 2, true};
                    [3] = {"C#", 3, 2, 2, true};
                    [4] = {"D#", 3, 2, 2, true};
                    [5] = {"F", 3, 2, 2, true};
                    [6] = {"F#", 3, 2, 2, true};
                    [7] = {"G#", 3, 2, 2, true};
                    [8] = {"A", 3, 2, 2, true};
                    [9] = {"A#", 3, 2, 2, true};
                    [10] = {"G#", 3, 2, 2, true};
                    [11] = {"F#", 3, 2, 2, true};
                    [12] = {"F", 3, 2, 2, true};
                    [13] = {"D#", 3, 2, 2, true};
                    [14] = {"C#", 3, 2, 2, true};
                    [15] = {"C", 3, 2, 2, true};
                    [16] = {"A#", 2, 2, 2, true}
                }
            }
        };
        ["Measures"] = {
            1
        }
    }
}

local function playSong(song)
    local tempo = 60 / song["Tempo"]
    local channels = song["Channels"]
    local patterns = song["Patterns"]
    local measures = song["Measures"]
    for i = 1, #channels do
        task.spawn(function()
            local currentMeasure = 1
            local currentPattern = measures[currentMeasure]
            local currentNote = 0
            
            while true do
                currentNote += 1
                currentPattern = measures[currentMeasure]
                
                if currentNote > #patterns[currentPattern][i] then
                    currentMeasure += 1
                    if currentMeasure > #measures then
                        currentMeasure = 1
                    end
                    currentNote = 1
                    currentPattern = measures[currentMeasure]
                end
                
                local note = patterns[currentPattern][i][currentNote]
                local sound
                if patterns[currentPattern][i][currentNote] ~= nil then
                    sound = playSound(channels[i], {note[1], note[2]}, note[3])
                end
                task.wait(tempo / note[4])
                sound.Playing = false
            end
        end)
    end
end

playSong(sounds["NightSong"])
INFO