/
main.go
330 lines (276 loc) · 8.48 KB
/
main.go
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
package main
import (
"flag"
"fmt"
"log"
"math"
"os"
"strings"
"github.com/go-gl/glfw/v3.3/glfw"
"github.com/gordonklaus/portaudio"
"github.com/inkyblackness/imgui-go/v4"
"github.com/kaishuu0123/chibines/chibines"
"github.com/kaishuu0123/chibines/internal/audio"
"github.com/kaishuu0123/chibines/internal/gui"
)
const (
WINDOW_WIDTH int = 640
WINDOW_HEIGHT int = 480
)
var (
windowFlags imgui.WindowFlags = imgui.WindowFlagsNoCollapse |
imgui.WindowFlagsNoMove |
imgui.WindowFlagsNoResize |
imgui.WindowFlagsNoTitleBar
)
type NSFInfoForView struct {
title string
artist string
copyright string
}
const FreqC0 = 32.7032
var NoteNames = []string{
"C",
"C#",
"D",
"D#",
"E",
"F",
"F#",
"G",
"G#",
"A",
"A#",
"B",
}
var window *gui.MasterWindow
var nsfPlayer *chibines.NSFPlayer
var audioForConsole *audio.Audio
var isRunning bool = false
var nsfInfoForView *NSFInfoForView
func NoteFromFreq(freq float64) float64 {
return 12.0 * math.Log2(freq/FreqC0)
}
func NoteString(noteValue int) string {
note := (noteValue - 1) % 12
octave := (noteValue - 1) / 12
return fmt.Sprintf("%s%d", NoteNames[note], octave)
}
func Freq2NoteString(freq float32) string {
if freq < FreqC0 {
return ""
}
noteFloat := NoteFromFreq(float64(freq))
note := int(math.Round(noteFloat))
// cents := math.Round((noteFloat - note) * 100.0)
return NoteString(note + 1)
}
func StartAudio() {
// initialize audio
portaudio.Initialize()
audioForConsole = audio.NewAudio()
if err := audioForConsole.Start(); err != nil {
log.Fatalln(err)
}
if nsfPlayer.Console == nil {
log.Fatalln("console must be set")
}
nsfPlayer.Console.SetAudioChannel(audioForConsole.Channel)
nsfPlayer.Console.SetAudioSampleRate(audioForConsole.SampleRate)
}
func StopAudio() {
if isRunning {
audioForConsole.Stop()
nsfPlayer.Console.SetAudioSampleRate(0)
nsfPlayer.Console.SetAudioChannel(nil)
portaudio.Terminate()
}
}
func ResetNSFPlayer(file_name string) {
StopAudio()
isRunning = false
log.Println("Reset Console")
log.Printf("NSF file path: %s\n", file_name)
var err error
nsfPlayer, err = chibines.NewNSFPlayer(file_name)
if err != nil {
log.Fatalln(err)
}
log.Printf("TotalSongs: %d\n", nsfPlayer.NSFFileInfo.TotalSongs)
log.Printf("SongName: %s\n", nsfPlayer.NSFFileInfo.SongName)
log.Printf("ArtistName: %s\n", nsfPlayer.NSFFileInfo.ArtistName)
log.Printf("Copyright: %s\n", nsfPlayer.NSFFileInfo.CopyrightHolder)
nsfInfoForView = &NSFInfoForView{
title: fmt.Sprintf("%-9s : %s", "Title", nsfPlayer.NSFFileInfo.SongName),
artist: fmt.Sprintf("%-9s : %s", "Artist", nsfPlayer.NSFFileInfo.ArtistName),
copyright: fmt.Sprintf("%-9s : %s", "Copyright", nsfPlayer.NSFFileInfo.CopyrightHolder),
}
isRunning = true
StartAudio()
}
func onDrop(names []string) {
var sb strings.Builder
sb.WriteString(fmt.Sprintf("%s", names[0]))
dropInFiles := sb.String()
ResetNSFPlayer(dropInFiles)
}
func renderNSFPlayerGUI() {
imgui.PushFont(window.FontsData[1])
imgui.SetNextWindowPos(imgui.Vec2{X: 0, Y: 0})
imgui.SetNextWindowSize(imgui.Vec2{X: float32(WINDOW_WIDTH), Y: float32(WINDOW_HEIGHT)})
// NSF File Information
imgui.BeginV("Window", nil, windowFlags)
imgui.Text(nsfInfoForView.title)
imgui.Text(nsfInfoForView.artist)
imgui.Text(nsfInfoForView.copyright)
trackAndState := fmt.Sprintf("%-9s : %02d", "No", nsfPlayer.CurrentSong+1)
imgui.Text(trackAndState)
dl := imgui.WindowDrawList()
pos := imgui.CursorPos()
imgui.PushFont(window.FontsData[2])
imgui.SetCursorPos(imgui.Vec2{X: pos.X, Y: pos.Y + 20})
pos = imgui.CursorPos()
freq := nsfPlayer.Console.APU.CurrentInfo()
// Draw Visualizer (Keyboard & Noize & DMC)
s1 := Freq2NoteString(freq.Square1)
square1Text := fmt.Sprintf("Square 1: %s", s1)
imgui.Text(square1Text)
var s1KeyIndex int = -1
if s1 != "" {
s1KeyIndex = Pitch2KeyIndexTable[s1]
}
DrawPianoKeyboard(&dl, imgui.Vec2{X: pos.X + 0, Y: pos.Y + 25}, s1KeyIndex)
imgui.SetCursorPos(imgui.Vec2{X: pos.X, Y: pos.Y + 80})
pos = imgui.CursorPos()
s2 := Freq2NoteString(freq.Square2)
square2Text := fmt.Sprintf("Square 2: %s", s2)
var s2KeyIndex int = -1
if s2 != "" {
s2KeyIndex = Pitch2KeyIndexTable[s2]
}
imgui.Text(square2Text)
DrawPianoKeyboard(&dl, imgui.Vec2{X: pos.X + 0, Y: pos.Y + 25}, s2KeyIndex)
imgui.SetCursorPos(imgui.Vec2{X: pos.X, Y: pos.Y + 80})
pos = imgui.CursorPos()
t := Freq2NoteString(freq.Triangle)
triangleText := fmt.Sprintf("Triangle: %s", t)
var triangleKeyIndex int = -1
if t != "" {
triangleKeyIndex = Pitch2KeyIndexTable[t]
}
imgui.Text(triangleText)
DrawPianoKeyboard(&dl, imgui.Vec2{X: pos.X + 0, Y: pos.Y + 25}, triangleKeyIndex)
imgui.SetCursorPos(imgui.Vec2{X: pos.X, Y: pos.Y + 80})
pos = imgui.CursorPos()
noiseText := fmt.Sprintf("Noise: Volume = %X Period = %d", freq.Noise.Out, freq.Noise.Period)
imgui.Text(noiseText)
dmcText := fmt.Sprintf("DMC : Volume = %X Period = %d", freq.DMC.Out, freq.DMC.Period)
imgui.Text(dmcText)
// Status Line (Play Status & Help Text)
pos = imgui.CursorPos()
// XXX: For Debug
// io := imgui.CurrentIO()
// framerateText := fmt.Sprintf("Application average %.3f ms/frame (%.1f FPS)", 1000.0/io.Framerate(), io.Framerate())
// imgui.Text(framerateText)
var state string = "Stopped"
if nsfPlayer.PlayState {
state = "Playing"
}
stateText := fmt.Sprintf("State: %s", state)
textSize := imgui.CalcTextSize(stateText, false, 0)
pos = imgui.CursorPos()
posX := pos.X
posY := imgui.WindowHeight() - (textSize.Y * 2)
imgui.SetCursorPos(imgui.Vec2{X: posX, Y: posY})
imgui.Text(stateText)
imgui.SameLine()
helpText := fmt.Sprintf("Start/Stop = Enter | Prev = <- | Next = ->")
textSize = imgui.CalcTextSize(helpText, false, 0)
posX = imgui.WindowContentRegionWidth() - float32(textSize.X)
pos = imgui.CursorPos()
imgui.SetCursorPos(imgui.Vec2{X: posX, Y: pos.Y})
imgui.Text(helpText)
imgui.PopFont()
imgui.End()
imgui.PopFont()
}
func renderGUI(w *gui.MasterWindow) {
w.Platform.NewFrame()
imgui.NewFrame()
if isRunning {
renderNSFPlayerGUI()
} else {
var msg string = "ChibiNES NSF Player is currently stopped.\n\nPlease drag and drop NSF file."
textSize := imgui.CalcTextSize(msg, false, 0)
xpos := (float32(WINDOW_WIDTH) - textSize.X) / 2
ypos := (float32(WINDOW_HEIGHT) - textSize.Y) / 2
imgui.ForegroundDrawList().
AddText(
imgui.Vec2{X: xpos, Y: ypos},
imgui.PackedColor(0xFFFFFFFF),
msg,
)
}
imgui.Render()
w.Renderer.PreRender(w.ClearColor)
w.Renderer.Render(w.Platform.DisplaySize(), w.Platform.FramebufferSize(), imgui.RenderedDrawData())
w.Platform.PostRender()
}
func main() {
flag.Parse()
if len(flag.Args()) >= 1 {
_, err := os.Stat(flag.Arg(0))
if err != nil {
log.Fatalln("no NSF file specified or found")
}
ResetNSFPlayer(flag.Arg(0))
}
defer StopAudio()
window = gui.NewMasterWindow("ChibiNES NSF Player", WINDOW_WIDTH, WINDOW_HEIGHT, 0)
window.SetDropCallback(onDrop)
defer window.Renderer.Dispose()
defer window.Platform.Dispose()
style := imgui.CurrentStyle()
style.SetColor(imgui.StyleColorWindowBg, imgui.Vec4{X: 0.0, Y: 0.0, Z: 0.0, W: 1.00})
initPianoKeyboard()
// Unlimit FPS
glfw.SwapInterval(0)
prev_timestamp := glfw.GetTime()
previousKeyState := map[int]bool{
int(glfw.KeyRight): false,
int(glfw.KeyLeft): false,
int(glfw.KeyEnter): false,
}
glfwWindow := window.Platform.Window
for !window.Platform.ShouldStop() {
cur_timestamp := glfw.GetTime()
window.Platform.ProcessEvents()
if !previousKeyState[int(glfw.KeyRight)] && glfwWindow.GetKey(glfw.KeyRight) == glfw.Press {
previousKeyState[int(glfw.KeyRight)] = true
nsfPlayer.NextSong()
}
if previousKeyState[int(glfw.KeyRight)] && glfwWindow.GetKey(glfw.KeyRight) == glfw.Release {
previousKeyState[int(glfw.KeyRight)] = false
}
if !previousKeyState[int(glfw.KeyLeft)] && glfwWindow.GetKey(glfw.KeyLeft) == glfw.Press {
previousKeyState[int(glfw.KeyLeft)] = true
nsfPlayer.PrevSong()
}
if previousKeyState[int(glfw.KeyLeft)] && glfwWindow.GetKey(glfw.KeyLeft) == glfw.Release {
previousKeyState[int(glfw.KeyLeft)] = false
}
if !previousKeyState[int(glfw.KeyEnter)] && glfwWindow.GetKey(glfw.KeyEnter) == glfw.Press {
previousKeyState[int(glfw.KeyEnter)] = true
nsfPlayer.PlayState = !nsfPlayer.PlayState
}
if previousKeyState[int(glfw.KeyEnter)] && glfwWindow.GetKey(glfw.KeyEnter) == glfw.Release {
previousKeyState[int(glfw.KeyEnter)] = false
}
dt := cur_timestamp - prev_timestamp
prev_timestamp = cur_timestamp
if isRunning && nsfPlayer.PlayState {
nsfPlayer.StepSeconds(dt)
}
renderGUI(window)
}
}