-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.go
More file actions
415 lines (324 loc) · 10.6 KB
/
main.go
File metadata and controls
415 lines (324 loc) · 10.6 KB
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
package main
import (
"embed"
"flag"
"fmt"
"log"
"os"
"runtime"
"strings"
"time"
"github.com/faiface/beep"
"github.com/faiface/beep/mp3"
"github.com/faiface/beep/speaker"
)
//go:embed audio_files/*.mp3
var default_audio_sounds embed.FS
type SoundAlias int
const (
READY_GO SoundAlias = iota
STARTING SoundAlias = iota
WORKOUT SoundAlias = iota
PREPARE_FOR SoundAlias = iota
REST SoundAlias = iota
WORKOUT_ENDED SoundAlias = iota
PHASE_ENDED SoundAlias = iota
)
type SoundStream struct {
Stream beep.StreamSeekCloser
Format beep.Format
}
type Manager struct {
DefaultSounds map[SoundAlias]SoundStream
UserSounds map[string]SoundStream
CurrentSessionName string
// This approach is chosen so we could easily restart the session from a specific phase and workout in future updates
CurrentPhaseIndex int
CurrentWorkoutIndex int
}
func InitManager() *Manager {
m := &Manager{
DefaultSounds: map[SoundAlias]SoundStream{},
UserSounds: map[string]SoundStream{},
}
m.init_default_sounds()
sound := m.DefaultSounds[READY_GO]
speaker.Init(sound.Format.SampleRate, sound.Format.SampleRate.N(time.Second/10))
return m
}
func main() {
workout_session_in_minutes := flag.String("d", "", "How many minutes you like this session last, d=duration")
workout_time_in_seconds := flag.String("w", "45", "Provide workout time, in seconds, w=workout")
rest_time_in_seconds := flag.String("r", "0", "Provide rest time between sets, in seconds, if not provided no rest, r=rest")
flag.Usage = func() {
fmt.Fprintln(os.Stderr, "trainr have two modes, from file")
fmt.Fprintln(os.Stderr, "trainer ./[woroutfile]")
fmt.Fprintln(os.Stderr, "or cmd args, when you want just a quick exercise you can use: ")
flag.PrintDefaults()
fmt.Fprintln(os.Stderr, "Default: d=infinite r=0, w=45 ")
fmt.Fprintln(os.Stderr, "\nExample:")
fmt.Fprintln(os.Stdin, "-d 10 -r 15 w 20")
}
flag.Parse()
m := InitManager()
if len(flag.Args()) > 0 {
m.lunch_training_from_phases(flag.Arg(0))
} else {
m.lunch_training_from_cmd(workout_session_in_minutes, workout_time_in_seconds, rest_time_in_seconds)
}
}
func (m *Manager) lunch_training_from_phases(filepath string) {
parser := InitParser(filepath, "")
parser.create_workout_config()
directory_to_read := parser.current_config.DefaultDumpFolder + parser.current_config.SessionName
present_audio_files, read_directory_err := os.ReadDir(directory_to_read)
if read_directory_err != nil {
PrintFl("[ERROR] read custom audio directory: %v ", read_directory_err.Error())
log.Fatal()
}
for _, file := range present_audio_files {
file_name := strings.TrimSuffix(file.Name(), ".mp3")
map_reference := parser.current_config.SessionName + "/" + file_name
m.UserSounds[map_reference] = sound_to_stream(map_reference, parser.current_config.DefaultDumpFolder)
}
m.CurrentSessionName = parser.current_config.SessionName
current_session := parser.current_config
phase_traker := make(chan struct{}, 1)
between_phase_traker := make(chan struct{}, 1)
workout_traker := make(chan struct{}, 1)
rest_traker := make(chan struct{}, 1)
end_traker := make(chan struct{}, 1)
for _, phase := range current_session.Phases {
for _, workout := range phase.Workouts {
// when time is set we do not edit it
if workout.TimeWorkout == "" {
if phase.TimeWorkout != "" {
workout.TimeWorkout = phase.TimeWorkout
} else {
workout.TimeWorkout = current_session.TimeWorkout
}
}
if workout.TimeRest == "" {
if phase.TimeRest != "" {
workout.TimeRest = phase.TimeRest
} else {
workout.TimeRest = current_session.TimeRest
}
}
}
}
MAIN_SESSION_LOOP:
for {
select {
case <-phase_traker:
phase := current_session.Phases[m.CurrentPhaseIndex]
m.play_sound_current_session(phase.PhaseName)
PrintFl("phase_traker %v", phase.PhaseName)
//just to postpone the workout name sound start from the session name
time.Sleep(time.Second * 2)
workout_traker <- struct{}{}
case <-workout_traker:
phase := current_session.Phases[m.CurrentPhaseIndex]
workout := phase.Workouts[m.CurrentWorkoutIndex]
m.play_sound(m.DefaultSounds[STARTING])
m.play_sound_current_session(workout.WorkoutName)
// m.play_sound(m.DefaultSounds[READY_GO])
workout_timer_any_accepted_time(workout.TimeWorkout)
if m.CurrentWorkoutIndex < len(phase.Workouts)-1 {
if workout.TimeRest != "" {
rest_traker <- struct{}{}
} else {
m.play_sound(m.DefaultSounds[PREPARE_FOR])
next_workout := phase.Workouts[m.CurrentWorkoutIndex+1]
m.play_sound_current_session(next_workout.WorkoutName)
m.CurrentWorkoutIndex++
workout_traker <- struct{}{}
}
} else {
if m.CurrentPhaseIndex < len(current_session.Phases)-1 {
if current_session.TimePhaseRest != "" {
between_phase_traker <- struct{}{}
} else {
m.CurrentPhaseIndex++
m.CurrentWorkoutIndex = 0
phase_traker <- struct{}{}
}
} else {
end_traker <- struct{}{}
}
}
case <-rest_traker:
phase := current_session.Phases[m.CurrentPhaseIndex]
workout := phase.Workouts[m.CurrentWorkoutIndex]
m.play_sound(m.DefaultSounds[REST])
time.Sleep(time.Second * 1)
m.play_sound(m.DefaultSounds[PREPARE_FOR])
next_workout := phase.Workouts[m.CurrentWorkoutIndex+1]
m.play_sound_current_session(next_workout.WorkoutName)
workout_timer_any_accepted_time(workout.TimeRest)
m.CurrentWorkoutIndex++
workout_traker <- struct{}{}
case <-between_phase_traker:
PHASE_ENDED_SOUND := m.DefaultSounds[PHASE_ENDED]
m.play_sound(PHASE_ENDED_SOUND)
workout_timer_any_accepted_time(current_session.TimePhaseRest)
m.CurrentPhaseIndex++
m.CurrentWorkoutIndex = 0
phase_traker <- struct{}{}
case <-end_traker:
WORKOUT_ENDED_SOUND := m.DefaultSounds[WORKOUT_ENDED]
m.play_sound(WORKOUT_ENDED_SOUND)
break MAIN_SESSION_LOOP
default:
m.play_sound(m.DefaultSounds[READY_GO])
phase_traker <- struct{}{}
}
}
}
func (m *Manager) lunch_training_from_cmd(workout_session_in_minutes, workout_time_in_seconds, rest_time_in_seconds *string) {
fmt.Printf("rest_time_in_seconds: %v \n", *rest_time_in_seconds)
fmt.Printf("workout_time_in_seconds: %v \n", *workout_time_in_seconds)
is_workout_phase := true
is_init := true
m.play_sound(m.DefaultSounds[READY_GO])
end_workout := make(chan struct{})
if workout_session_in_minutes != nil && *workout_session_in_minutes != "" {
go func() {
workout_timer(*workout_session_in_minutes, MINUTES)
end_workout <- struct{}{}
}()
}
is_workout_ended := false
MAIN_LOOP:
for {
select {
case <-end_workout:
is_workout_ended = true
default:
if is_workout_ended {
break MAIN_LOOP
}
if is_workout_phase {
// TODO: this is ugly as hell, make it better
if !is_init {
WORKOUT_SOUND := m.DefaultSounds[WORKOUT]
m.play_sound(WORKOUT_SOUND)
}
fmt.Printf("WORKOUT phase t: %v \n", time.Now().Format(time.TimeOnly))
if is_init {
is_init = false
}
workout_timer(*workout_time_in_seconds, SECONDS)
is_workout_phase = false
} else {
if *rest_time_in_seconds != "0" {
REST_SOUND := m.DefaultSounds[REST]
m.play_sound(REST_SOUND)
fmt.Printf("REST phase t: %v \n", time.Now().Format(time.TimeOnly))
workout_timer(*rest_time_in_seconds, SECONDS)
}
is_workout_phase = true
}
}
}
WORKOUT_ENDED_SOUND := m.DefaultSounds[WORKOUT_ENDED]
m.play_sound(WORKOUT_ENDED_SOUND)
}
func sound_to_stream(sound_name, base_folder string) SoundStream {
workout_file, err := os.Open(base_folder + sound_name + ".mp3")
if err != nil {
log.Fatal(err)
}
streamer, format, err := mp3.Decode(workout_file)
if err != nil {
log.Fatal(err)
}
return SoundStream{
Stream: streamer,
Format: format,
}
}
// TODO: Will be enabled later
// func sound_from_embeded(sound_name string) SoundStream {
//
// workout_file, err := default_audio_sounds.Open("sounds/" + sound_name + ".mp3")
//
// if err != nil {
// log.Fatal(err)
// }
//
// streamer, format, err := mp3.Decode(workout_file)
// if err != nil {
// log.Fatal(err)
// }
//
// return SoundStream{
// Stream: streamer,
// Format: format,
// }
// }
func (m *Manager) play_sound_current_session(file_name string) {
m.play_sound(m.UserSounds[m.CurrentSessionName+"/"+file_name])
}
func (m *Manager) init_default_sounds() {
// TODO: this should work for final build
// m.DefaultSounds[REST] = sound_from_embeded("rest")
// m.DefaultSounds[WORKOUT] = sound_from_embeded("workout")
// m.DefaultSounds[READY_GO] = sound_from_embeded("ready_go")
// m.DefaultSounds[WORKOUT_ENDED] = sound_from_embeded("end_workout")
// TODO: imbed this default sounds in to the binary
// old way
default_sounds_folder := "./audio_files/"
m.DefaultSounds[READY_GO] = sound_to_stream("ready_go", default_sounds_folder)
m.DefaultSounds[STARTING] = sound_to_stream("starting", default_sounds_folder)
m.DefaultSounds[WORKOUT] = sound_to_stream("workout", default_sounds_folder)
m.DefaultSounds[PREPARE_FOR] = sound_to_stream("prepare_for", default_sounds_folder)
m.DefaultSounds[REST] = sound_to_stream("rest", default_sounds_folder)
m.DefaultSounds[WORKOUT_ENDED] = sound_to_stream("end_workout", default_sounds_folder)
m.DefaultSounds[PHASE_ENDED] = sound_to_stream("phase_ended_rest", default_sounds_folder)
}
func (m *Manager) play_sound(sound SoundStream) {
done := make(chan bool)
speaker.Play(beep.Seq(sound.Stream, beep.Callback(func() {
sound.Stream.Seek(0)
done <- true
})))
<-done
}
type TimeDuration string
const (
MINUTES TimeDuration = "m"
SECONDS TimeDuration = "s"
)
func workout_timer(time_in_seconds string, duration_to_parce TimeDuration) {
workout_time_in_seconds, duration_parse_err := time.ParseDuration(time_in_seconds + string(duration_to_parce))
if duration_parse_err != nil {
fmt.Printf("Cannot parse workout time")
log.Fatal("")
}
time.Sleep(workout_time_in_seconds)
}
func workout_timer_any_accepted_time(time_to_convert string) {
workout_timer, duration_parse_err := time.ParseDuration(time_to_convert)
if duration_parse_err != nil {
fmt.Printf("[Error] Cannot parse workout time %v", duration_parse_err)
log.Fatal("")
}
PrintFl("Duration: %v", workout_timer)
time.Sleep(workout_timer)
}
func PrintFl(format string, a ...any) {
fmt.Println()
_, f_name, f_line, _ := runtime.Caller(1)
fmt.Printf("%v:%v \n", f_name, f_line)
fmt.Printf(format, a...)
fmt.Println()
}
func TODO(format ...any) {
fmt.Println()
_, f_name, f_line, _ := runtime.Caller(1)
fmt.Printf("%v:%v \n", f_name, f_line)
fmt.Printf("%s", format...)
fmt.Println()
log.Fatal()
}