Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,13 @@ docker run -d -p 8080:8080 -v /path/to/your/music:/music darep/beatstream:latest

Open http://0.0.0.0:8080 on your browser. Log in and wait when indexing ends, refresh page and happy listening!

For very large libraries, you can give Go a soft memory limit while leaving headroom for TagLib and the rest of the
container. For example, with a 1 GiB container limit:

```bash
docker run -d --memory=1g -e GOMEMLIMIT=900MiB -p 8080:8080 -v /path/to/your/music:/music darep/beatstream:latest
```

### Manual Install

Requirements: Go 1.22 or newer. Node.js 20 or newer. TagLib (C bindings) e.g. libtagc
Expand Down
29 changes: 25 additions & 4 deletions api.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
package main

import (
"bufio"
"encoding/json"
"io"
"io/fs"
"net/http"
"os"
Expand Down Expand Up @@ -267,7 +269,7 @@ func refreshSongs() error {

// Sort songs
sort.Slice(songs, func(i, j int) bool {
return songs[i].ToNaturalSortString() < songs[j].ToNaturalSortString()
return compareSongs(&songs[i], &songs[j]) < 0
})

// lock "songs.json" so that it's not read while being written
Expand All @@ -282,10 +284,29 @@ func refreshSongs() error {
defer file.Close()

logger.Log.Println("Writing songs to file…")
err = json.NewEncoder(file).Encode(songs)
if err != nil {
return writeSongsJSON(file, songs)
}

func writeSongsJSON(w io.Writer, songs []Song) error {
buffer := bufio.NewWriter(w)
if err := buffer.WriteByte('['); err != nil {
return err
}

return nil
encoder := json.NewEncoder(buffer)
for i := range songs {
if i > 0 {
if err := buffer.WriteByte(','); err != nil {
return err
}
}
if err := encoder.Encode(&songs[i]); err != nil {
return err
}
}

if err := buffer.WriteByte(']'); err != nil {
return err
}
return buffer.Flush()
}
25 changes: 24 additions & 1 deletion api_test.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,11 @@
package main

import "testing"
import (
"bytes"
"encoding/json"
"reflect"
"testing"
)

func TestDeleteOtherSessions(t *testing.T) {
sessions = []Session{
Expand All @@ -15,3 +20,21 @@ func TestDeleteOtherSessions(t *testing.T) {
t.Fatalf("unexpected remaining sessions: %#v", sessions)
}
}

func TestWriteSongsJSON(t *testing.T) {
track := 3
want := []Song{{Filename: "song.mp3", Path: "/song.mp3", Title: "Song", Artist: "Artist", TrackNum: &track, Length: 90}}
var output bytes.Buffer

if err := writeSongsJSON(&output, want); err != nil {
t.Fatal(err)
}

var got []Song
if err := json.Unmarshal(output.Bytes(), &got); err != nil {
t.Fatalf("invalid JSON %q: %v", output.String(), err)
}
if !reflect.DeepEqual(got, want) {
t.Fatalf("got %#v, want %#v", got, want)
}
}
1 change: 0 additions & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -5,5 +5,4 @@ go 1.23.5
require (
github.com/go-chi/chi/v5 v5.2.3
github.com/joho/godotenv v1.5.1
github.com/wtolson/go-taglib v0.0.0-20210406152913-79209c280058
)
2 changes: 0 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -2,5 +2,3 @@ github.com/go-chi/chi/v5 v5.2.3 h1:WQIt9uxdsAbgIYgid+BpYc+liqQZGMHRaUwp0JUcvdE=
github.com/go-chi/chi/v5 v5.2.3/go.mod h1:L2yAIGWB3H+phAw1NxKwWM+7eUH/lU8pOMm5hHcoops=
github.com/joho/godotenv v1.5.1 h1:7eLL/+HRGLY0ldzfGMeQkb7vMd0as4CfYvUVzLqw0N0=
github.com/joho/godotenv v1.5.1/go.mod h1:f4LDr5Voq0i2e/R5DDNOoa2zzDfwtkZa6DnEwAbqwq4=
github.com/wtolson/go-taglib v0.0.0-20210406152913-79209c280058 h1:/kj9W8wSHTlwt/i4n6902i/YOPYNIXiDR/PAmgbrDyc=
github.com/wtolson/go-taglib v0.0.0-20210406152913-79209c280058/go.mod h1:p+WHGfN/a+Ol37Pm7EIOO/6Cylieb2qn1jmKfxtSsUg=
2 changes: 1 addition & 1 deletion helpers.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import (
"strings"

"github.com/Darep/Beatstream/logger"
tag "github.com/wtolson/go-taglib"
tag "github.com/Darep/Beatstream/taglib"
)

// Helper for responding as JSON
Expand Down
35 changes: 17 additions & 18 deletions song.go
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
package main

import (
"cmp"
"encoding/json"
"fmt"
"strings"
)

type Song struct {
Expand All @@ -16,27 +16,26 @@ type Song struct {
Length int `json:"length"`
}

// For sorting songs in a natural way (artist, album, track number)
func (song *Song) ToNaturalSortString() string {
sortables := []string{}

if song.Artist != "" {
sortables = append(sortables, song.Artist)
func compareSongs(a, b *Song) int {
if result := cmp.Compare(a.Artist, b.Artist); result != 0 {
return result
}
if song.Album != "" {
sortables = append(sortables, song.Album)
if result := cmp.Compare(a.Album, b.Album); result != 0 {
return result
}
if song.TrackNum == nil {
sortables = append(sortables, song.Title)
} else {
sortables = append(sortables, fmt.Sprintf("%03d", *song.TrackNum))
if a.TrackNum != nil && b.TrackNum != nil {
if result := cmp.Compare(*a.TrackNum, *b.TrackNum); result != 0 {
return result
}
} else if a.TrackNum != nil {
return -1
} else if b.TrackNum != nil {
return 1
}

if len(sortables) > 0 {
return strings.Join(sortables, " ")
if result := cmp.Compare(a.Title, b.Title); result != 0 {
return result
}

return song.Filename
return cmp.Compare(a.Filename, b.Filename)
}

// Return a nice title for the song, like "Artist - Title". Fallback to the filename
Expand Down
22 changes: 22 additions & 0 deletions song_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
package main

import (
"sort"
"testing"
)

func TestCompareSongs(t *testing.T) {
one, two := 1, 2
songs := []Song{
{Artist: "B", Album: "Album", TrackNum: &one, Title: "First"},
{Artist: "A", Album: "Album", Title: "No track"},
{Artist: "A", Album: "Album", TrackNum: &two, Title: "Second"},
{Artist: "A", Album: "Album", TrackNum: &one, Title: "First"},
}

sort.Slice(songs, func(i, j int) bool { return compareSongs(&songs[i], &songs[j]) < 0 })

if songs[0].Title != "First" || songs[1].Title != "Second" || songs[2].Title != "No track" || songs[3].Artist != "B" {
t.Fatalf("unexpected order: %#v", songs)
}
}
11 changes: 11 additions & 0 deletions taglib/LICENSE
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
Copyright 2012-2021 William Trevor Olson

Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met:

1. Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer.

2. Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution.

3. Neither the name of the copyright holder nor the names of its contributors may be used to endorse or promote products derived from this software without specific prior written permission.

THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
101 changes: 101 additions & 0 deletions taglib/taglib.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,101 @@
package taglib

// #cgo pkg-config: taglib
// #cgo LDFLAGS: -ltag_c
// #include <stdlib.h>
// #include <tag_c.h>
import "C"

import (
"errors"
"sync"
"time"
"unsafe"
)

var (
ErrInvalid = errors.New("invalid file")
mutex sync.Mutex
)

type File struct {
file *C.TagLib_File
tag *C.TagLib_Tag
properties *C.TagLib_AudioProperties
}

func init() {
C.taglib_id3v2_set_default_text_encoding(3)
C.taglib_set_string_management_enabled(0)
}

func Read(filename string) (*File, error) {
mutex.Lock()
defer mutex.Unlock()

name := C.CString(filename)
defer C.free(unsafe.Pointer(name))

file := C.taglib_file_new(name)
if file == nil {
return nil, ErrInvalid
}
if C.taglib_file_is_valid(file) == 0 {
C.taglib_file_free(file)
return nil, ErrInvalid
}

return &File{
file: file,
tag: C.taglib_file_tag(file),
properties: C.taglib_file_audioproperties(file),
}, nil
}

func (file *File) Close() {
mutex.Lock()
defer mutex.Unlock()

C.taglib_file_free(file.file)
file.file = nil
file.tag = nil
file.properties = nil
}

func (file *File) Title() string {
mutex.Lock()
defer mutex.Unlock()
return goString(C.taglib_tag_title(file.tag))
}

func (file *File) Artist() string {
mutex.Lock()
defer mutex.Unlock()
return goString(C.taglib_tag_artist(file.tag))
}

func (file *File) Album() string {
mutex.Lock()
defer mutex.Unlock()
return goString(C.taglib_tag_album(file.tag))
}

func (file *File) Track() int {
mutex.Lock()
defer mutex.Unlock()
return int(C.taglib_tag_track(file.tag))
}

func (file *File) Length() time.Duration {
mutex.Lock()
defer mutex.Unlock()
return time.Duration(C.taglib_audioproperties_length(file.properties)) * time.Second
}

func goString(value *C.char) string {
if value == nil {
return ""
}
defer C.free(unsafe.Pointer(value))
return C.GoString(value)
}
14 changes: 14 additions & 0 deletions taglib/taglib_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
package taglib

import (
"errors"
"path/filepath"
"testing"
)

func TestReadMissingFile(t *testing.T) {
path := filepath.Join(t.TempDir(), "missing.mp3")
if _, err := Read(path); !errors.Is(err, ErrInvalid) {
t.Fatalf("got %v, want %v", err, ErrInvalid)
}
}