streaming

This commit is contained in:
gauthiier
2019-01-03 14:21:24 +01:00
parent 5e4f9b8862
commit 683cce60ab
5 changed files with 175 additions and 94 deletions
+69 -29
View File
@@ -13,12 +13,15 @@ import (
var pln = log.Println
type Pop func(*Playlist)
type Playlist struct {
NAME string
CTRACK config.Track_t
CALBUM config.Album_t
LIST []uint32
MAX int
CALLB Pop
}
type PrettyPlaylist struct {
@@ -29,6 +32,19 @@ type PrettyPlaylist struct {
MAX int
}
type MinimalTrack struct {
ID string `json:"id"`
NAME string `json:"name"`
MAKER string `json:"maker"`
ALBUM string `json:"album"`
}
type MinimalPlaylist struct {
NAME string `json:"name"`
CTRACK string `json:"ctrack"`
CALBUM string `json:"calbum"`
LIST []MinimalTrack `json:"list"`
}
func MakeRandom(name string, max int) (*Playlist, error) {
err := archive.Build()
@@ -80,19 +96,22 @@ func (pp *PrettyPlaylist) Unpretty() *Playlist {
p := &Playlist{NAME: pp.NAME, CTRACK: pp.CTRACK, CALBUM: pp.CALBUM, MAX: pp.MAX}
p.LIST = make([]uint32, len(pp.LIST))
for i := 0; i < len(pp.LIST); i++ {
p.LIST = append(p.LIST, pp.LIST[i].ID)
p.LIST[i] = pp.LIST[i].ID
}
return p
}
// type PrettyPlaylist struct {
// NAME string
// CTRACK config.Track_t
// CALBUM config.Album_t
// LIST []config.Track_t
// MAX int
// }
func (p *Playlist) Minimal() *MinimalPlaylist {
mp := &MinimalPlaylist{NAME: p.NAME, CTRACK: p.CTRACK.NAME}
mp.CALBUM = p.CALBUM.MAKER + " - " + p.CALBUM.NAME
mp.LIST = make([]MinimalTrack, len(p.LIST))
for i := 0; i < len(p.LIST); i++ {
track := archive.Archive_map[p.LIST[i]]
strid := strconv.FormatUint(uint64(track.ID), 10)
mp.LIST[i] = MinimalTrack{ID: strid, NAME: track.NAME, MAKER: track.MAKER, ALBUM: track.ALBUM}
}
return mp
}
func (pp *PrettyPlaylist) Print() {
@@ -115,9 +134,9 @@ func print(slice []uint32) {
// https://github.com/golang/go/wiki/SliceTricks
// https://stackoverflow.com/questions/33834742/remove-and-adding-elements-to-array-in-go-lang
func (p *Playlist) Pop() error {
func (p *Playlist) Pop() (string, error) {
if len(p.LIST) == 0 {
return errors.New("Playlist is empty")
return "", errors.New("Playlist is empty")
}
t := p.LIST[0]
@@ -130,7 +149,11 @@ func (p *Playlist) Pop() error {
p.CTRACK = archive.Archive_map[t]
p.CALBUM = config.Xcfg.Archive.ALBUMS[p.CTRACK.AID]
return nil
if p.CALLB != nil {
p.CALLB(p)
}
return p.CTRACK.PATH, nil
}
func (p *Playlist) Push(track_id uint32) error {
@@ -191,26 +214,43 @@ func (p *Playlist) Move(track_id uint32, at_index int) error {
return errors.New("Invalid track ID")
}
slice_head := p.LIST[:k]
slice_move := p.LIST[k+1:at_index+1]
slice_tail := make([]uint32, len(p.LIST[at_index+1:]))
copy(slice_tail, p.LIST[at_index+1:])
// pln("k: " + strconv.Itoa(k))
// pln("at_index: " + strconv.Itoa(at_index))
// pln("------")
// slice_move = append(slice_move, track_id)
var slice_head, slice_move, slice_tail []uint32
if k < at_index {
slice_head = make([]uint32, len(p.LIST[:k]))
copy(slice_head, p.LIST[:k])
// slice_head = p.LIST[:k]
slice_move = make([]uint32, len(p.LIST[k+1:at_index+1]))
copy(slice_move, p.LIST[k+1:at_index+1])
// slice_move = p.LIST[k:at_index]
slice_tail = make([]uint32, len(p.LIST[at_index+1:]))
copy(slice_tail, p.LIST[at_index+1:])
p.LIST = append(slice_head, append(append(slice_move, track_id), slice_tail...)...)
} else if k > at_index {
slice_head = make([]uint32, len(p.LIST[:at_index]))
copy(slice_head, p.LIST[:at_index])
// slice_head = p.LIST[:at_index]
slice_move = make([]uint32, len(p.LIST[at_index:k]))
copy(slice_move, p.LIST[at_index:k])
// slice_tail = p.LIST[k+1:]
slice_tail = make([]uint32, len(p.LIST[k+1:]))
copy(slice_tail, p.LIST[k+1:])
p.LIST = append(append(append(slice_head, track_id), slice_move...), slice_tail...)
}
pln("from_index: " + strconv.Itoa(k))
pln("at_index: " + strconv.Itoa(at_index))
pln("------")
pln("slice head: ")
print(slice_head)
pln("slice move: ")
print(slice_move)
pln("** ")
pln("slice tail: ")
print(slice_tail)
pln("...........................")
p.LIST = append(slice_head, append(append(slice_move, track_id), slice_tail...)...)
// pln("slice head: ")
// print(slice_head)
// pln("slice move: ")
// print(slice_move)
// pln("** ")
// pln("slice tail: ")
// print(slice_tail)
// pln("...........................")
return nil
}