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
|
package main
// The spotify backend is based on the librespot-golang code released under MIT
// license.
// Authors of the librespot-golang code are:
// Copyright (c) 2015 Paul Lietar
// Copyright (c) 2015 badfortrains
// Copyright (c) 2018 Guillaume "xplodwild" Lesniak
import (
"errors"
"log"
"fmt"
"os"
"strings"
"sync"
"unsafe"
"github.com/librespot-org/librespot-golang/Spotify"
"github.com/librespot-org/librespot-golang/librespot"
"github.com/librespot-org/librespot-golang/librespot/core"
"github.com/librespot-org/librespot-golang/librespot/utils"
"github.com/xlab/portaudio-go/portaudio"
"github.com/xlab/vorbis-go/decoder"
)
const (
deviceName = "wrms"
// The number of samples per channel in the decoded audio
samplesPerChannel = 2048
// The samples bit depth
bitDepth = 16
// The samples format
sampleFormat = portaudio.PaFloat32
)
type SpotifyBackend struct {
session *core.Session
}
func NewSpotify() (*SpotifyBackend, error) {
spotify := SpotifyBackend{}
if err := portaudio.Initialize(); paError(err) {
return nil, errors.New(paErrorText(err))
}
var err error
username := os.Getenv("WRMS_SPOTIFY_USER")
password := os.Getenv("WRMS_SPOTIFY_PASSWORD")
if username == "" || password == "" {
return nil, errors.New("User and password for spotify must be provided")
}
spotify.session, err = librespot.Login(username, password, deviceName)
if err != nil {
return nil, err
}
return &spotify, nil
}
func (spotify *SpotifyBackend) Play(song *Song) {
trackID := song.Uri
session := spotify.session
fmt.Println("Loading track for play: ", trackID)
// Get the track metadata: it holds information about which files and encodings are available
track, err := session.Mercury().GetTrack(utils.Base62ToHex(trackID))
if err != nil {
fmt.Println("Error loading track: ", err)
return
}
fmt.Println("Track:", track.GetName())
// As a demo, select the OGG 160kbps variant of the track. The "high quality" setting in the official Spotify
// app is the OGG 320kbps variant.
var selectedFile *Spotify.AudioFile
for _, file := range track.GetFile() {
if file.GetFormat() == Spotify.AudioFile_OGG_VORBIS_160 {
selectedFile = file
}
}
// Synchronously load the track
audioFile, err := session.Player().LoadTrack(selectedFile, track.GetGid())
// TODO: channel to be notified of chunks downloaded (or reader?)
if err != nil {
fmt.Printf("Error while loading track: %s\n", err)
} else {
// We have the track audio, let's play it! Initialize the OGG decoder, and start a PortAudio stream.
// Note that we skip the first 167 bytes as it is a Spotify-specific header. You can decode it by
// using this: https://sourceforge.net/p/despotify/code/HEAD/tree/java/trunk/src/main/java/se/despotify/client/player/SpotifyOggHeader.java
fmt.Println("Setting up OGG decoder...")
dec, err := decoder.New(audioFile, samplesPerChannel)
if err != nil {
log.Fatalln(err)
}
info := dec.Info()
go func() {
dec.Decode()
dec.Close()
}()
fmt.Println("Setting up PortAudio stream...")
fmt.Printf("PortAudio channels: %d / SampleRate: %f\n", info.Channels, info.SampleRate)
var wg sync.WaitGroup
var stream *portaudio.Stream
callback := paCallback(&wg, int(info.Channels), dec.SamplesOut())
if err := portaudio.OpenDefaultStream(&stream, 0, info.Channels, sampleFormat, info.SampleRate,
samplesPerChannel, callback, nil); paError(err) {
log.Fatalln(paErrorText(err))
}
fmt.Println("Starting playback...")
if err := portaudio.StartStream(stream); paError(err) {
log.Fatalln(paErrorText(err))
}
wg.Wait()
}
}
func (spotify *SpotifyBackend) Pause() {
}
func (spotify *SpotifyBackend) Search(keyword string) []Song {
session := spotify.session
resp, err := session.Mercury().Search(keyword, 12, session.Country(), session.Username())
if err != nil {
fmt.Println("Failed to search:", err)
return []Song{}
}
res := resp.Results
fmt.Println("Search results for ", keyword)
fmt.Println("=============================")
if res.Error != nil {
fmt.Println("Search result error:", res.Error)
}
fmt.Printf("\nTracks: %d (total %d)\n", len(res.Tracks.Hits), res.Tracks.Total)
results := []Song{}
for _, track := range res.Tracks.Hits {
uriParts := strings.Split(track.Uri, ":")
results = append(results, NewSong(track.Name, track.Artists[0].Name, "spotify", uriParts[len(uriParts) - 1]))
// fmt.Printf(" => %v (%s)\n", track, track.Uri)
}
return results
}
// PortAudio helpers
func paError(err portaudio.Error) bool {
return portaudio.ErrorCode(err) != portaudio.PaNoError
}
func paErrorText(err portaudio.Error) string {
return "PortAudio error: " + portaudio.GetErrorText(err)
}
func paCallback(wg *sync.WaitGroup, channels int, samples <-chan [][]float32) portaudio.StreamCallback {
wg.Add(1)
return func(_ unsafe.Pointer, output unsafe.Pointer, sampleCount uint,
_ *portaudio.StreamCallbackTimeInfo, _ portaudio.StreamCallbackFlags, _ unsafe.Pointer) int32 {
const (
statusContinue = int32(portaudio.PaContinue)
statusComplete = int32(portaudio.PaComplete)
)
frame, ok := <-samples
if !ok {
wg.Done()
return statusComplete
}
if len(frame) > int(sampleCount) {
frame = frame[:sampleCount]
}
var idx int
out := (*(*[1 << 32]float32)(unsafe.Pointer(output)))[:int(sampleCount)*channels]
for _, sample := range frame {
if len(sample) > channels {
sample = sample[:channels]
}
for i := range sample {
out[idx] = sample[i]
idx++
}
}
return statusContinue
}
}
|