Skip to content

Commit

Permalink
added position control
Browse files Browse the repository at this point in the history
  • Loading branch information
Grantley Cullar authored and Grantley Cullar committed Nov 13, 2022
1 parent 0a237e1 commit 5acf548
Show file tree
Hide file tree
Showing 2 changed files with 55 additions and 13 deletions.
4 changes: 2 additions & 2 deletions internal/menu.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ func DynamicHeight(items []string) int {
if len(items) >= 10 {
return 12
}
return len(items)+2
return len(items) + 2
}

// Menu list all the audio files from the path set from newpath then play the selected file
Expand Down Expand Up @@ -99,7 +99,7 @@ func Menu() {
l.Rows = items
l.SelectedRowStyle = ui.NewStyle(ui.Color(SelectedRowThemeColor))
l.WrapText = false
l.SetRect(0, 0, 40, DynamicHeight(items))
l.SetRect(0, 0, 50, DynamicHeight(items))
l.TitleStyle.Fg = ui.Color(TitleThemeColor)
l.BorderStyle.Fg = ui.Color(BorderThemeColor)

Expand Down
64 changes: 53 additions & 11 deletions internal/player.go
Original file line number Diff line number Diff line change
Expand Up @@ -109,64 +109,103 @@ func AudioPlayer(file string, name string) {
p := widgets.NewParagraph()
p.Title = "Playing"
p.Text = name
p.SetRect(0, 0, 40, 3)
p.SetRect(0, 0, 50, 3)
p.TitleStyle.Fg = ui.Color(TitleThemeColor)
p.BorderStyle.Fg = ui.Color(BorderThemeColor)

posGauge := widgets.NewGauge()
posGauge.Title = "Position"
posGauge.Percent = 0
posGauge.SetRect(0, 3, 50, 6)
posGauge.TitleStyle.Fg = ui.Color(TitleThemeColor)
posGauge.BorderStyle.Fg = ui.Color(BorderThemeColor)

volGauge := widgets.NewGauge()
volGauge.Title = "Volume"
volGauge.Percent = 50
volGauge.SetRect(0, 3, 40, 6)
volGauge.SetRect(0, 6, 50, 9)
volGauge.TitleStyle.Fg = ui.Color(TitleThemeColor)
volGauge.BorderStyle.Fg = ui.Color(BorderThemeColor)

speedGauge := widgets.NewGauge()
speedGauge.Title = "Speed"
speedGauge.Percent = 50
speedGauge.SetRect(0, 6, 40, 9)
speedGauge.SetRect(0, 9, 50, 12)
speedGauge.TitleStyle.Fg = ui.Color(TitleThemeColor)
speedGauge.BorderStyle.Fg = ui.Color(BorderThemeColor)

c := widgets.NewParagraph()
c.Text = `Pause / Play: [ENTER]
Position: [← →]
Volume: [↓ ↑]
Speed: [← →]
Speed: [A / S]
Normal Speed: [N]
Back to Menu: [Backspace]
Back to Menu: [BACKSPACE]
`
c.SetRect(0, 9, 40, 16)
c.SetRect(0, 12, 50, 19)
c.BorderStyle.Fg = ui.Color(BorderThemeColor)

uiEvents := ui.PollEvents()
ticker := time.NewTicker(time.Second).C

draw := func() {
ui.Render(p, c, volGauge, speedGauge)
ui.Render(p, c, posGauge, volGauge, speedGauge)
}

// Detect keys
for {
newPos := streamer.Position()

position := format.SampleRate.D(streamer.Position())
length := format.SampleRate.D(streamer.Len())

positionStatus := 100 * (float32(position.Round(time.Second)) / float32(length.Round(time.Second)))
posGauge.Percent = int(positionStatus)

select {
case e := <-uiEvents:
switch e.ID {
case "<Enter>": // pause audio
ctrl.Paused = !ctrl.Paused

case "<Up>": // increase volume
volume.Volume += 0.1
volGauge.Percent += 2

case "<Down>": // decrease volume
volume.Volume -= 0.1
volGauge.Percent -= 2
case "<Right>": // increase speed by x1.1
speedy.SetRatio(speedy.Ratio() + 0.1)
speedGauge.Percent += 2
case "<Left>": // decrease speed by x1.1

case "<Left>", "<Right>":
speaker.Lock()
if e.ID == "<Left>" {
newPos -= int(format.SampleRate.N(time.Second))
} else if e.ID == "<Right>" {
newPos += int(format.SampleRate.N(time.Second))
}
if newPos < 0 {
newPos = 0
}
if newPos >= streamer.Len() {
newPos = streamer.Len() - 1
}
if err := streamer.Seek(newPos); err != nil {
fmt.Println(err)
}
speaker.Unlock()

case "a":
speedy.SetRatio(speedy.Ratio() - 0.1)
speedGauge.Percent -= 2

case "s":
speedy.SetRatio(speedy.Ratio() + 0.1)
speedGauge.Percent += 2

case "n": // Normalize speed
speedy.SetRatio(1)
speedGauge.Percent = 50

case "<C-<Backspace>>": // go back to menu
ctrl.Paused = !ctrl.Paused
Menu()
Expand All @@ -180,12 +219,15 @@ Back to Menu: [Backspace]
case volume.Volume >= 2:
volume.Volume = 2
volGauge.Percent = 100

case volume.Volume <= -2:
volume.Volume = -2
volGauge.Percent = 0

case speedy.Ratio() >= 2:
speedy.SetRatio(2)
speedGauge.Percent = 100

case speedy.Ratio() <= 0.5:
speedy.SetRatio(0.5)
speedGauge.Percent = 0
Expand Down

0 comments on commit 5acf548

Please sign in to comment.