info = { type = "image", title = "Slap bass", category = "other", author = "BRRRO", version = "1.0", license = "MIT", desc = "A bass slaps a groove, notes rise from the amp", api = 33, } local STEP_HZ = 30 local FR = 0 local tr = ({ sk = { ["Body colour"] = "Farba tela" }, cs = { ["Body colour"] = "Barva těla" }, })[lang] or {} local function T(s) return tr[s] or s end settings = { { key = "body", label = T("Body colour"), type = "color", default = "#1EB4A0" }, { key = "bpm", label = T("Tempo"), type = "int", min = 60, max = 180, unit = " BPM", default = 104 }, } local rect, px, blend, blend_rect = gfx.rect, gfx.px, gfx.blend, gfx.blend_rect local floor, sin, cos, sqrt, ceil = math.floor, math.sin, math.cos, math.sqrt, math.ceil local hash32 = math.hash32 local W, H = gfx.W, gfx.H local WALL, SPOT1, SPOT2 = { 38, 22, 74 }, { 58, 34, 104 }, { 82, 52, 136 } local OUTL = { 22, 14, 38 } local FLOOR, FLOOR_HI = { 74, 44, 42 }, { 124, 80, 62 } local SEAM = { 48, 26, 30 } local NECK, FRET, INLAY = { 98, 56, 34 }, { 172, 172, 184 }, { 234, 226, 202 } local NUT = { 242, 234, 210 } local GUARD, GUARD_S = { 80, 40, 30 }, { 128, 70, 42 } local PICK = { 22, 20, 26 } local METAL, METAL_D = { 202, 202, 214 }, { 118, 118, 134 } local TOLEX, TOLEX_HI = { 30, 28, 36 }, { 78, 72, 88 } local GRILL, PANEL, KNOB = { 66, 60, 78 }, { 198, 172, 112 }, { 34, 30, 30 } local LED_OFF, LED_ON = { 112, 22, 30 }, { 255, 72, 72 } local SURR, CONE, CONE_HI = { 20, 18, 26 }, { 94, 90, 108 }, { 132, 128, 150 } local CAP, CAP_HI = { 152, 148, 166 }, { 224, 222, 236 } local CABLE = { 196, 52, 78 } local STR, STR_V, STR_G = { 178, 178, 192 }, { 248, 246, 236 }, { 206, 206, 220 } local SPARK = { 255, 228, 120 } local NOTE_C = { { 255, 98, 178 }, { 255, 214, 64 }, { 72, 222, 255 }, { 158, 240, 86 } } local C, CL, CD = {}, {}, {} local function body_colours(c) C = { c.r, c.g, c.b } CL = { c.r + (255 - c.r) * 2 // 5, c.g + (255 - c.g) * 2 // 5, c.b + (255 - c.b) * 2 // 5 } CD = { c.r * 5 // 8, c.g * 5 // 8, c.b * 5 // 8 } end body_colours({ r = 30, g = 180, b = 160 }) local BG, SPK = nil, nil function on_settings() body_colours(cfg.body or { r = 30, g = 180, b = 160 }) BG = nil end local BODY = { { 36, 9, 11 }, { 37, 8, 12 }, { 38, 8, 13 }, { 38, 29, 31 }, { 39, 7, 13 }, { 39, 28, 32 }, { 40, 7, 14 }, { 40, 26, 32 }, { 41, 7, 33 }, { 42, 7, 33 }, { 43, 8, 33 }, { 44, 9, 32 }, { 45, 10, 31 }, { 46, 10, 31 }, { 47, 9, 32 }, { 48, 8, 33 }, { 49, 7, 34 }, { 50, 6, 34 }, { 51, 6, 34 }, { 52, 6, 34 }, { 53, 6, 34 }, { 54, 6, 34 }, { 55, 6, 34 }, { 56, 6, 34 }, { 57, 7, 33 }, { 58, 7, 33 }, { 59, 8, 32 }, { 60, 9, 31 }, { 61, 11, 29 }, { 62, 13, 27 }, } local HEADST = { { 1, 16, 24 }, { 2, 15, 25 }, { 3, 15, 25 }, { 4, 15, 25 }, { 5, 15, 25 }, { 6, 15, 25 }, { 7, 15, 25 }, { 8, 15, 25 }, { 9, 16, 24 }, { 10, 16, 24 }, } local GUARD_R = { { 41, 12, 28 }, { 42, 11, 29 }, { 43, 11, 29 }, { 44, 12, 28 }, { 45, 13, 27 }, { 46, 14, 26 }, { 47, 14, 27 }, { 48, 14, 28 }, { 49, 15, 28 }, { 50, 15, 28 }, { 51, 16, 27 }, { 52, 18, 26 }, } local FRETS = { 16, 20, 24, 27, 30, 33, 35, 37, 39 } local INLAYS = { 20, 18, 20, 22, 20, 28, 20, 31, 18, 36, 22, 36 } local SX = { 17, 19, 21, 23 } local TOP = { 2, 6, 6, 2 } local Y0, Y1 = 12, 56 local ENV = {} for y = Y0, Y1 do ENV[y] = sin(3.14159 * (y - Y0 + 1) / (Y1 - Y0 + 2)) end local function c3(c) return c[1], c[2], c[3] end local function rows(t, c, grow) for i = 1, #t do local r = t[i] if grow then rect(r[2] - 1, r[1] - 1, r[3] - r[2] + 3, 3, c3(c)) else rect(r[2], r[1], r[3] - r[2] + 1, 1, c3(c)) end end end local function disc(cx, cy, r, c) for y = floor(cy - r), ceil(cy + r) do local dy = y + 0.5 - cy if dy * dy < r * r then local hw = sqrt(r * r - dy * dy) local x0, x1 = ceil(cx - hw - 0.5), floor(cx + hw - 0.5) if x1 >= x0 then rect(x0, y, x1 - x0 + 1, 1, c3(c)) end end end end local function speaker(pump) disc(51, 48, 9.5, SURR) disc(51, 48, pump and 9.0 or 8.5, pump and CONE_HI or CONE) disc(51, 48, pump and 4.6 or 3.6, SURR) disc(51, 48, pump and 3.7 or 2.7, CAP) px(49, 46, c3(CAP_HI)) if pump then px(50, 45, c3(CAP_HI)); rect(45, 42, 3, 1, c3(CAP_HI)) end end local function build() rect(0, 0, W, H, c3(WALL)) disc(21, 25, 26, SPOT1) disc(21, 25, 19, SPOT2) rect(0, 60, W, 4, c3(FLOOR)) rect(0, 60, W, 1, c3(FLOOR_HI)) rect(9, 61, 1, 3, c3(SEAM)); rect(36, 61, 1, 3, c3(SEAM)); rect(57, 61, 1, 3, c3(SEAM)) rect(40, 31, 22, 1, c3(TOLEX_HI)) rect(39, 32, 24, 28, c3(TOLEX)) rect(46, 28, 10, 1, c3(TOLEX_HI)); rect(46, 29, 1, 2, c3(TOLEX_HI)); rect(55, 29, 1, 2, c3(TOLEX_HI)) rect(41, 33, 20, 4, c3(PANEL)) for k = 0, 3 do rect(43 + 4 * k, 34, 2, 2, c3(KNOB)) end rect(57, 34, 2, 2, c3(LED_OFF)) rect(41, 38, 20, 20, c3(GRILL)) rect(41, 60, 3, 1, c3(TOLEX)); rect(58, 60, 3, 1, c3(TOLEX)) speaker(true) SPK = gfx.snapshot(41, 38, 20, 20) speaker(false) local CB = { 35, 55, 36, 56, 36, 57, 37, 58, 37, 59, 38, 60 } for i = 1, #CB, 2 do px(CB[i], CB[i + 1], c3(CABLE)) end rows(BODY, OUTL, true) rows(BODY, C) for i = 1, #BODY do local r = BODY[i] px(r[2], r[1], c3(CL)) rect(r[3] - 1, r[1], 2, 1, c3(CD)) end rows(HEADST, OUTL, true) rows(HEADST, C) rect(16, 1, 1, 9, c3(CL)) rect(12, 2, 3, 2, c3(METAL)); rect(12, 6, 3, 2, c3(METAL)) rect(26, 2, 3, 2, c3(METAL)); rect(26, 6, 3, 2, c3(METAL)) rect(15, 11, 11, 30, c3(OUTL)) rect(16, 12, 9, 29, c3(NECK)) rect(16, 11, 9, 1, c3(NUT)) for i = 1, #FRETS do rect(16, FRETS[i], 9, 1, c3(FRET)) end for i = 1, #INLAYS, 2 do px(INLAYS[i], INLAYS[i + 1], c3(INLAY)) end for i = 1, 4 do local x, t = SX[i], TOP[i] rect(x, t, 1, 11 - t, c3(STR)) rect(x - (x > 20 and 1 or 0), t - 1, 2, 2, c3(METAL)) end rows(GUARD_R, OUTL, true) rows(GUARD_R, GUARD) for i = 1, 9 do local h = hash32(i * 0x9E3779B1) local r = GUARD_R[1 + math.umod(h, #GUARD_R)] px(r[2] + math.umod(h >> 8, r[3] - r[2] + 1), r[1], c3(GUARD_S)) end rect(16, 46, 5, 2, c3(PICK)); rect(20, 49, 5, 2, c3(PICK)) rect(15, 56, 11, 3, c3(METAL_D)); rect(16, 56, 9, 2, c3(METAL)) rect(28, 54, 2, 2, c3(METAL)); rect(31, 57, 2, 2, c3(METAL)) px(34, 55, c3(METAL_D)) BG = gfx.snapshot(0, 0, W, H) end local PAT = { 4, 0, 4, 3, 2, 0, 4, 1, 4, 0, 3, 4, 1, 0, 2, 3 } local DEC, A0 = 40, 1.8 local AGE = { 0, 0, 0, 0 } local function runs(x, a, c, alpha) local y0, o0 = Y0, floor(a * ENV[Y0] + 0.5) for y = Y0 + 1, Y1 + 1 do local o = y <= Y1 and floor(a * ENV[y] + 0.5) or 99 if o ~= o0 then if not alpha then rect(x + o0, y0, 1, y - y0, c[1], c[2], c[3]) elseif o0 ~= 0 then blend_rect(x + o0, y0, 1, y - y0, c[1], c[2], c[3], alpha) end y0, o0 = y, o end end end local function note(x, y, c, beam) local r, g, b = c[1], c[2], c[3] if beam then rect(x, y + 5, 3, 2, r, g, b); rect(x + 5, y + 4, 3, 2, r, g, b) rect(x + 2, y, 1, 6, r, g, b); rect(x + 7, y - 1, 1, 6, r, g, b) rect(x + 2, y, 3, 1, r, g, b); rect(x + 5, y - 1, 3, 1, r, g, b) else rect(x, y + 5, 3, 2, r, g, b); rect(x + 2, y, 1, 6, r, g, b) px(x + 3, y + 1, r, g, b); px(x + 4, y + 2, r, g, b) end end function render() FR = frame * STEP_HZ // gfx.FPS if not BG then build() end gfx.blit_rgb(BG, 0, 0, W, H) local bpm = cfg.bpm or 104 local u = FR * bpm local s = u // (STEP_HZ * 30) local into = (u % (STEP_HZ * 30)) / bpm local step = STEP_HZ * 30 / bpm for i = 1, 4 do AGE[i] = -1 end for k = 0, 15 do local st = PAT[(s - k) % 16 + 1] if st > 0 and AGE[st] < 0 then AGE[st] = k * step + into end end if PAT[s % 16 + 1] > 0 and into < 4 then gfx.blit_rgb(SPK, 41, 38, 20, 20) rect(57, 34, 2, 2, c3(LED_ON)) end for i = 1, 4 do local age, x = AGE[i], SX[i] local a = (age >= 0 and age < DEC) and A0 * (1 - age / DEC) or 0 if a < 0.5 then rect(x, Y0, 1, Y1 - Y0 + 1, c3(STR)) else local amp = a * cos(age * 1.9) runs(x, -amp, STR_G, 0.45) runs(x, amp, age < 8 and STR_V or STR) end if age >= 0 and age < 5 then local r, al = 1 + floor(age), 1 - age / 5 blend(x - r, 39, SPARK[1], SPARK[2], SPARK[3], al) blend(x + r, 39, SPARK[1], SPARK[2], SPARK[3], al) blend(x, 39 - r, SPARK[1], SPARK[2], SPARK[3], al) if age < 2 then px(x, 39, 255, 255, 255) end end end gfx.clip(37, 0, 27, 28) local q = s // 2 for k = 0, 3 do local qq = q - k local y = floor(31 - ((s - 2 * qq) * step + into)) if y > -8 and y < 28 then local h = hash32(qq * 0x9E3779B1) & 0x7FFF note(39 + h % 17, y, NOTE_C[(h >> 5) % 4 + 1], ((h >> 8) & 1) == 1) end end gfx.clip() end