forked from ericclack/racket-examples
-
Notifications
You must be signed in to change notification settings - Fork 0
/
learn-music-phrase1.rkt
267 lines (223 loc) · 7.8 KB
/
learn-music-phrase1.rkt
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
#lang racket
#|
Show notes, then play them, see if you get them right!
Press any key if you find the note easy, you'll then
get less time to play this note next time.
Challenge: How to avoid practicing mistakes or quick
corrections, wait for the player to remember first?
CHANGES from v3:
- Add drills to practice phrases of notes:
(note, note+1 note+2)
(note, note+2, note+4)
etc
- Show current note and the next one
- Add back in easy-note logic for phrases - play them less
often
TODO:
- Fix e2 ledger lines
- Bigger display, scalable with some variable
- Reverse phrases randomly
- Save easy-notes for next time, so that the player
doesn't need to edit the source code
- Fix display of ledgers
|#
(require srfi/1)
(require 2htdp/universe 2htdp/image)
(require 2htdp/image)
(require rsound)
(require rsound/piano-tones)
(require "util.rkt")
(require racket/trace)
;; What notes do we want to practice?
(define NOTES
'(e2 f2 g2 a3 b3 c3 d3 e3 f3 g3 a4 b4 c4 d4 e4 f4 g4 a5 b5 c5))
;; We need MIDI numbers to play them, these are the standard set
(define PIANO-MIDI-NOTES
'(52 53 55 57 59 60 62 64 65 67 69 71 72 74 76 77 79 81 83 84))
;; Guitar midi notes are one octave lower
(define MIDI-NOTES
(map (λ (x) (- x 12)) PIANO-MIDI-NOTES))
;; We want to show the open string notes differently
(define OPEN-STRINGS
'(e2 a3 d3 g3 b4 e4))
;; The initial set of easy phrases for *me* to play - change this
;; to suit your needs
(define EASY-PHRASES
'((a3 b3 c3) (e4 f4 g4) (a4 b4 c4) (e3 e4 b4) (e3 g3 b4) (g3 a4 b4) (d3 e3 f3)))
;; How likely to skip easy phrases (0-1)?
(define SKIP-EASY-PHRASES 0.7)
;; The canvas
(define WIDTH 400)
(define HEIGHT 300)
(define G-CLEF (bitmap "GClef.png"))
;; How many seconds between notes? Change this to suit your needs
(define TICK-RATE 0.75)
(define PIX-PER-NOTE 11)
(define PIX-BETWEEN-LINES (* 2 PIX-PER-NOTE))
;; - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
(define (note-index a-note)
(list-index (curry equal? a-note) NOTES))
(define above/align-left
((curry above/align) "left"))
(define (stave)
(overlay/offset
(scale 0.53 G-CLEF)
120 -6
(overlay
(apply above/align-left
(cons (line 300 0 "black")
(times-repeat 4
(above/align-left
(line 0 20 "black")
(line 300 0 "black")
))))
(empty-scene WIDTH HEIGHT "white"))))
(define (note-pos-relative-b4 a-note)
;; b4 is the middle of the stave
;; b4 = 0, a4 = -1, c4 = 1, etc
(- (note-index a-note) PIX-PER-NOTE))
(define (note-y-pos a-note)
(* PIX-PER-NOTE (note-pos-relative-b4 a-note)))
(define (ledger-line)
(line 30 0 "black"))
(define (ledger-lines a-note)
(define num-lines
(/ (- (abs (note-pos-relative-b4 a-note)) 4) 2))
(define ledger-images
(times-repeat num-lines (ledger-line)))
(define ledger-lines-img
(foldr (λ (i scene) (above i (empty-scene 0 PIX-BETWEEN-LINES) scene))
(empty-scene 0 0)
ledger-images))
(if (ledger-lines-above? a-note)
;; TODO: replace pix numbers with formulae
(overlay/align/offset
"middle" "bottom"
ledger-lines-img
0 193
(empty-scene 0 HEIGHT))
(overlay/align/offset
"middle" "top"
ledger-lines-img
0 -216
(empty-scene 0 HEIGHT))))
(define (ledger-lines-above? a-note)
;; Are the extenders above the stave (or below)?
(>= (note-pos-relative-b4 a-note) 0))
(define (note-img a-note)
;; A note, with a hint for open strings
(circle 10
(if (member a-note OPEN-STRINGS) "outline" "solid")
"black"))
(define (note+ledger-line-img a-note)
(overlay
(ledger-lines a-note)
(overlay/offset
(note-img a-note)
0 (note-y-pos a-note)
(empty-scene 0 HEIGHT))))
(define (note-phrase-img notes)
;; A sequence of notes including extenders
(foldr (λ (n scene) (beside (note+ledger-line-img n)
(empty-scene 10 0) scene))
(empty-scene 10 0)
notes))
(define (show-notes notes)
(overlay
(note-phrase-img notes)
(stave)))
(define (show-note a-note)
(show-notes (list a-note)))
(define (play-note a-note)
(play (piano-tone
(list-ref MIDI-NOTES (note-index a-note)))))
;; - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
(define (next-random-note last-note easy-notes)
;; Next random note, but not last-note
;; also play easy-notes less often
(define note (random-choice NOTES))
(if (or (eq? note last-note)
(and (member note easy-notes)
(< (random) SKIP-EASY-PHRASES)))
(next-random-note last-note easy-notes)
note))
(define (play-note-times a-note easy-notes)
(if (member a-note easy-notes) 2 4))
;; The procs next-note-phrase-??? return a sequence of notes
;; that make some kind of pleasing phrase
(define (random-note-phrase-123)
(random-note-phrase '(1 2 3)))
(define (random-note-phrase-135)
(random-note-phrase '(1 3 5)))
(define (pick-notes deltas notes)
(cond
[(empty? deltas) '()]
[else (cons (list-ref notes (- (car deltas) 1))
(pick-notes (cdr deltas) notes))]))
(define (random-note-phrase deltas)
(define first-note (next-random-note #f '()))
(define notes (member first-note NOTES))
(define max-delta (apply max deltas))
(if (< (length notes) max-delta)
(random-note-phrase deltas)
(pick-notes deltas notes)))
;; Which type of note phrase to use?
(define (next-note-phrase easy-phrases)
(define phrase
(random-note-phrase (random-choice '((1 2 3) (1 3 5) (1 5 3) (1 5 7)
(4 2 3 1) (1 8 5)))))
(if (and (member phrase easy-phrases)
(< (random) SKIP-EASY-PHRASES))
(next-note-phrase easy-phrases)
phrase))
;; - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
;; big-bang world
(struct world (note notes-left phrase plays easy-phrases) #:transparent)
(define (next-note w)
;; Play the next note from the current phrase, repeat the phrase
;; or generate a new one.
(cond
[(empty? (world-notes-left w))
;; We've finished playing the phrase
(cond
[(= 1 (world-plays w))
;; Finished repeats, make a new phrase
(let* ([phrase (next-note-phrase (world-easy-phrases w))]
[plays 4])
(next-note (world (car phrase) phrase phrase
plays (world-easy-phrases w))))]
[else
;; More repeats left, restart phrase
(next-note (world #f (world-phrase w) (world-phrase w)
(sub1 (world-plays w)) (world-easy-phrases w)))])]
[else
;; We're still playing the phrase
(play-note (car (world-notes-left w)))
(world (car (world-notes-left w)) (cdr (world-notes-left w)) (world-phrase w)
(world-plays w) (world-easy-phrases w))]))
(define (easy-phrase w a-key)
;; The user finds the current note easy - stop playing it
;; and add it to the set
(let ((phrase (world-phrase w))
(easy-phrases (world-easy-phrases w)))
(world (world-note w) '() phrase 1
(if (member phrase easy-phrases)
easy-phrases
(cons phrase easy-phrases)))))
(define (render-scene w)
(place-image/align
(above/align "left"
(text (string-append "Easy phrases: "
(string-join (map (λ (x) (format "~a" x))
(world-easy-phrases w)) ", "))
15 "black")
(text "Press any key to add current note" 15 "black"))
5 5 "left" "top"
(show-notes (world-phrase w))))
(define (go)
(define phrase (next-note-phrase EASY-PHRASES))
(big-bang (world (car phrase) phrase phrase 4 EASY-PHRASES)
(on-tick next-note TICK-RATE)
(on-key easy-phrase)
(to-draw render-scene)))
(go)