aboutsummaryrefslogtreecommitdiff
path: root/intervals.lua
blob: e74a57514388eec7131a8d07bc73f4e9875ca305 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
-- interval practice
--
-- hear two notes;
-- play two notes
--
-- enc2: repeat
-- enc3: skip
--
-- to do:
-- 1) keep a score
-- 2) select scales
-- 3) select base tone
-- 4) key signatures
-- 5) play from input
-- 6) midi out
-- 7) parameters
--    - delay,
--    - midi out y/n
--    - amplitude

-- graphics and audio utilities
function midi_to_hz(note)
  local hz = (440 / 32) * (2 ^ ((note - 9) / 12))
  return hz
end

function interval_name(base, interval)
  t = {
    "unison",
    "minor second",
    "major second",
    "minor third",
    "major third",
    "perfect fourth",
    "diminished fifth",
    "perfect fifth",
    "minor sixth",
    "major sixth",
    "minor seventh",
    "major seventh",
    "octave"
  }
  return t[interval - base + 1]
end

function blit(x, y, bitmap, p)
  for i=1,#bitmap do
    for j=1,#bitmap[i] do
      local level = bitmap[i][j]
      if p and p.brightness then
        level = level * p.brightness
      else
        level = level * 10
      end
      if level > 0 then
        screen.level(math.floor(level))
        screen.pixel(x + j, y + i)
        screen.fill()

      end
    end
  end
end

function blit_center(x, y, sprite, p)
  local offset_before = sprite.offset_before or #sprite.data[1] / 2
  local offset_above = sprite.offset_above or #sprite.data / 2
  blit(x - offset_before, y - offset_above, sprite.data, p)
end

local quarter_sprite = {
  data={
    { 0,  0,  0,  0,  1,  0,  0},
    { 0,  0,  0,  0,  1,  0,  0},
    { 0,  0,  0,  0,  1,  0,  0},
    { 0,  0,  0,  0,  1,  0,  0},
    { 0,  0,  0,  0,  1,  0,  0},
    { 0,  0,  0,  0,  1,  0,  0},
    { 0,  1,  1,  1,  1,  0,  0},
    { 1,  1,  1,  1,  1,  0,  0},
    { 0,  1,  1,  1,  0,  0,  0},
  },
  -- metadata to center properly
  offset_before=2,
  offset_above=8
}

local sharp_sprite = {
  data = {
    { 0, 1, 0, 1, 0 },
    { 1, 1, 1, 1, 1 },
    { 0, 1, 0, 1, 0 },
    { 1, 1, 1, 1, 1 },
    { 0, 1, 0, 1, 0 }
  }
}

local flat_sprite = {
  data = {
    { 1, 0, 0, 0},
    { 1, 0, 0, 0},
    { 1, 1, 1, 0},
    { 1, 0, 1, 0},
    { 1, 1, 0, 0}
  }
}
-- parameters:
-- p.y: offset from the top
-- p.margin: distance from the side (since it's all centered)
-- p.notes: all quarter notes for now
function staff (p)
  local x_start = p.margin;
  local x_end = 128 - p.margin
  local width = 128 - p.margin * 2

  screen.level(1)
  for i=0,4 do
    for j=x_start,x_end do
      screen.pixel(j, p.y + 5 * i)
    end
  end
  screen.fill()

  for i=1,#p.notes do
    -- adjust for lines drawn
    local bottom_y = p.y + 20
    -- offset for this particular note
    local y = math.ceil(bottom_y - 2.5 * p.notes[i].place)

    -- evenly distribute along the x-axis
    local total_x = 128 - p.margin * 2
    local each_x = total_x / #p.notes
    local x = x_start + each_x * (i - 1) + each_x / 2

    -- draw the broken staff line, if it's out of range
    if y > bottom_y or y < p.y then
      screen.level(1) -- todo p.brightness
      screen.move(x - quarter_sprite.offset_before - 1, y + 1)
      screen.line(x + #quarter_sprite.data[1] - 1, y + 1)
      screen.stroke()
    end

    if p.notes[i].accidental > 0 then
      blit_center(x - 6, y, sharp_sprite, p.notes[i])
    elseif p.notes[i].accidental < 0 then
      blit_center(x - 6, y, flat_sprite, p.notes[i])
    end

    -- draw the note
    blit_center(x, y, quarter_sprite, p.notes[i])
  end
end

-- todo could probably do this more nicely
function c_major_treble(midi_note)
  local scale = {
    -- c / c#
    [60]={place=-2, accidental=0},
    [61]={place=-2, accidental=1},
    -- d / d#
    [62]={place=-1, accidental=0},
    [63]={place=-1, accidental=1},
    -- e 
    [64]={place=0, accidental=0},
    -- f / f#
    [65]={place=1, accidental=0},
    [66]={place=1, accidental=1},
    -- g / g#
    [67]={place=2, accidental=0},
    [68]={place=2, accidental=1},
    -- a / a#
    [69]={place=3, accidental=0},
    [70]={place=3, accidental=1},
    -- b
    [71]={place=4, accidental=0},
    -- c
    [72]={place=5, accidental=0},
  }
  return scale[midi_note]
end

-- little mode library...
-- a mode is a thing with fields for enc, redraw, and key
-- it can edit the state
function state_call(state, name, args)
  if state.stack[#state.stack] then
    local fn = state.stack[#state.stack][name]
    if fn then
      local ret = fn(table.unpack(args or {}))
      if ret then
        ret(state)
      end
    end
  else
    print("empty state?")
  end
end

function state_init(state)
  -- destroy old timer, create a new one.
  -- we need to do this because e.g. if we transition
  -- to the next screen halfway between 1 and 2, then we'll
  -- pause for half a second less than we ought to.
  if state.timer then
    state.timer:stop()
    metro.free(state.timer.id)
  end
  state.timer = metro.init()
  state.timer.time = state.tick_interval or 1.0
  state.timer.event = function(t)
    state_call(state, "tick", {t})
  end
  state_call(state, "init")
  state.timer:start()
end

function state_redraw(state)
  state_call(state, "redraw")
end

function pop(state)
  table.remove(state.stack)
  state_init(state)
end

function pop_n(n)
  return function (state)
    if state.timer then
      state.timer:stop()
    end
    for i=1,n,1 do
      table.remove(state.stack)
    end
    state_init(state)
  end
end

function replace(modes)
  return function (state)
    top = table.remove(state.stack)
    push(modes)(state)
  end
end

function push(modes)
  return function (state)
    for i=1,#modes do
      table.insert(state.stack, modes[i])
    end
    state_init(state)
  end
end

-- end mode library

engine.name = 'PolyPerc'
function play(hz)
  engine.hz(hz)
end


function message (msgs, tick_fn, midi_fn)
  return {
    init = function ()
      return state_redraw
    end,
    redraw=function ()
      screen.clear()
      local total_height = 10 * #msgs
      local y = (64 - total_height) / 2
      local x = 64
      for i=1,#msgs do
        screen.move(x, y + 10 * (i - 1) + 5)
        screen.text_center(msgs[i])
      end
      screen.update()
    end,
    key=function (n, x)
      if n == 3 and x == 0 then
        return pop
      end
    end,
    tick=function(t)
      if tick_fn then
        return tick_fn(t)
      end
    end,
    midi=function(ev)
      if midi_fn then
        return midi_fn(ev)
      end
    end
  }
end

function correct_guess(known, guess)
  return {
    init=function ()
      return state_redraw
    end,
    redraw=function()
      local known_note = c_major_treble(known)
      known_note.brightness = 10
      local guess_note = c_major_treble(guess)
      guess_note.brightness = 10
      staff({y=20, margin=5, notes={
        known_note, guess_note
      }})
      screen.move(64, 10)
      screen.text_center(interval_name(known, guess))
      screen.update()
    end,
    key=function(n, x)
      if n == 3 and x == 0 then
        return pop
      end
    end,
    tick = function()
      return pop
    end
  }
end

function play_and_wait_3 (known, guess)
  return {
    init=function ()
      return state_redraw
    end,
    redraw=function()
      screen.clear()
      local known_note = c_major_treble(known)
      known_note.brightness = 10
      staff({y=20, margin=5, notes={
        known_note,
        {place=0,accidental=0, brightness=0}
      }})
      screen.update()
    end,
    key=function(n, x)
      if n == 3 and x == 0 then
        return pop
      end
    end,
    midi=function(ev)
      if ev.type == "note_on" and ev.note == guess then
        return pop
      end
    end
  }
end

function play_and_wait_2 (known, guess)
  return {
    init=function ()
      play(midi_to_hz(guess))
      return state_redraw
    end,
    redraw=function()
      screen.clear()
      local known_note = c_major_treble(known)
      known_note.brightness = 5
      staff({y=20, margin=5, notes={
        known_note,
        {place=0,accidental=0, brightness=0}
      }})
      screen.update()
    end,
    key=function(n, x)
      -- forward button
      if n == 3 and x == 0 then
        return pop_n(2)
      end
      -- back button
      if n == 2 and x == 0 then
        return push({play_and_wait_1(known, guess)})
      end
    end,
    midi=function(ev)
      if ev.type == "note_on" and ev.note == known then
        return pop
      end
    end
  }
end

function play_and_wait_1 (midi_note)
  complete = nil
  return {
    init=function ()
      play(midi_to_hz(midi_note))
      return state_redraw
    end,
    redraw=function()
      screen.clear()
      local known_note = c_major_treble(midi_note)
      known_note.brightness = 5
      staff({y=20, margin=5, notes={
        known_note,
        {place=0,accidental=0, brightness=0}
      }})
      screen.update()
    end,
    tick=function()
      return pop
    end,
    key=function(n, x)
      -- forward button
      if n == 3 and x == 0 then
        return pop_n(3)
      end
      -- back button
      if n == 2 and x == 0 then
        print("back on 1")
        return replace({play_and_wait_1(midi_note)})
      end
    end,
  }
end

function choose_interval ()
  return {
    init=function ()

      local guess = math.random(60, 72)
      return push({
        correct_guess(60, guess),
        play_and_wait_3(60, guess),
        play_and_wait_2(60, guess),
        play_and_wait_1(60)
      })
    end
  }
end

-- drawing, state, etc
intervals_state = {
  stack = {
    choose_interval(),
    message({
      "first I'll play middle c.",
      "then a random major interval.",
      "play both notes back",
      "as best as you can"},
    function (t)
      if t > 2 then
        return pop
      end
    end),
    message({"yay."},
    function ()
      return pop
    end),
    message({
      "plug in a midi keyboard",
      "and give me a middle c."
    },
    nil,
    function (ev)
      if ev.type == "note_on" and ev.note == 60 then
        return pop
      end
    end)
  },
}

function init()
  play(midi_to_hz(60))
  -- initialize midi
  m = midi.connect()
  m.event = function (a)
    b = midi.to_msg(a)
    -- monitor
    if b.type == "note_on" then
      play(midi_to_hz(b.note))
    end
    state_call(intervals_state, "midi", {b})
  end
end

function cleanup()
  intervals_state.timer:stop()
end

function redraw()
  screen.clear()
  screen.aa(0)
  screen.line_width(1)

  state_call(intervals_state, "redraw")
end

function key(n, x)
  state_call(intervals_state, "key", {n, x})
end