musing 1.1.0

An MPD-inspired music server
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
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
# Musing: Docs

Musing listens for incoming connections on `localhost:PORT`, where `PORT` is 2137 by default, You can specify any other port either by a command-line argument or by an entry in the config file.

## Protocol
Musing exchanges data with clients through a TCP socket.
Every message must consist of the following:
- Exactly 4 bytes representing a 32-bit unsigned integer `N` (in big-endian).
- Then, exactly `N` bytes representing a string that parses to a JSON object.
Because every message in the Musing protocol is a JSON object at its core, whenever we write "a message/request contains the key `foo`" we mean that its JSON object contains the key `foo`.

After a client initiates the connection, it receives a message containing exactly one key `version` (the protocol's version is not relevant for now, but might be in the future when a breaking change occurs in the API).

Then, the client is free to send requests to Musing. Every request must contain a `kind` key (which allows Musing to distinguish endpoints) and zero or more additional keys specifying some additional arguments specific to any given request kind. All available requests are described in the next section.

Musing responds to every request with a response, which always contains a `status` key with a value of either `ok` or `err`. If `status` is `err`, then there will be a `reason` key present with a string value which describes why the request failed. Beyond that, responses may contain more keys specyfing details related to the given request. All responses are described in detail in the next section (with the `status`/`reason` keys ommitted for brevity). If a request doesn't have its response prototype listed, that means its response contains only the `status`/`reason` keys.

## Available requests

### ls
```json
{
    "kind": "ls",
    "dir": string,
}
```

Returns paths of all songs located in `dir`. The path can either be absolute or relative to the directory where the database is rooted. Only songs contained in the Musing database are taken into account. Returned paths are always absolute.
Should be used only with untagged/badly tagged music collections. With properly tagged collections using `select` will be more convenient.

Response:
```json
{
    "paths": array of strings,
}
```

Example request:
```json
{
    "kind": "ls",
    "dir": "/some/path",
}
```
Example response:
```json
{
    "status": "ok",
    "paths": ["/some/path/foo.mp3", "/some/path/bar.wav"],
}
```

### metadata
```json
{
    "kind": "metadata",
    "paths": array of strings,
    "tags": array of strings,
}
```
or
```json
{
    "kind": "metadata",
    "paths": array of strings,
    "all_tags": bool,
}
```

Returns objects containing key-value pairs representing the metadata of songs located in `paths`. If `tags` is specified, returns only the values corresponding to the provided tags. If `all_tags` is specified and true, returns values of all tags supported by Musing (you can find a list of supported tags at the end of these docs).

Response:
```json
{
    "metadata": array of objects,
}
```

Example request:
```json
{
    "kind": "metadata",
    "paths": ["/some/song.mp3", "/another/song.m4a"],
    "tags": ["artist"],
}
```
Example response:
```json
{
    "status": "ok",
    "metadata": [{"artist": "Foo Bar"}, {"artist": "Baz Qux"}],
}
```

### select
```json
{
    "kind": "select",
    "tags": array of strings,
    "filters": array of objects,
    "group_by": array of strings,
    "comparators": array of objects,
}
```

Returns paths and values of `tags` of those songs, which satisfy each of the `filters`. The results are grouped by the values of tags in `group_by` and sorted by `comparators`.

A filter is a JSON object with the following structure:
```json
{
    "kind": "regex",
    "tag": string,
    "regex": string,
}
```
As the name suggests, it allows only songs whose value of `tag` matches the regular expression `regex` to "pass through". If a song has no defined value for `tag`, it doesn't pass the filter. Regexes are parsed by the `regex` crate, so a reference of their syntax is available [here](https://docs.rs/regex/latest/regex/#syntax).

A comparator in a JSON object with the following structure:
```json
{
    "tag": string,
    "order": string,
}
```
Its job is to sort the response values according to the value of `tag`. The order is determined by the `order` key, whose only valid values are `"ascending"` and `"descending"`.

Response:
```json
{
    "values": array of objects,
}
```

Example request:
```json
{
    "kind": "select",
    "tags": ["tracktitle"],
    "filters": [
        {
            "kind": "regex",
            "tag": "albumartist",
            "regex": "^M.*",
        },
        {
            "kind": "regex",
            "tag": "album",
            "regex": "^M.*",
        },
    ],
    "group_by": ["album"],
    "comparators": [
        {
            "tag": "tracknumber",
            "order": "descending",
        },
    ],
}
```
Example response:
```json
{
    "status": "ok",
    "values": [
        {
            "album": "Metallica",
            "data": [
                ["Enter Sandman", "tracks/01_enter_sandman.mp3"],
                ["Sad but True", "tracks/02_sad_but_true.mp3"],
                ...
            ]
        },
        {
            "album": "Master of Puppets",
            "data": [
                ["Battery", "tracks/01_battery.mp3"],
                ["Master of Puppets", "tracks/02_master_of_puppets.mp3"],
                ...
            ]
        },
    ],
}
```

### update
```json
{
    "kind": "update",
}
```

Updates the music database, that is adds any files that have been created since the previous update, removes songs whose files don't exist anymore and re-adds songs whose metadata has changed.

### volume
```json
{
    "kind": "volume",
    "delta": integer,
}
```

Changes the volume by `delta` units. The resulting volume is clamped between 0 and 100.

### seek
```json
{
    "kind": "seek",
    "seconds": integer,
}
```

Seeks the audio by `seconds` seconds, backwards if the value is negative, forwards otherwise.

### speed
```json
{
    "kind": "speed",
    "delta": integer,
}
```

Changes the playback speed by `delta` percentage points. The resulting speed is clamped between 25 and 400.

### gapless
```json
{
    "kind": "gapless",
}
```

Toggles gapless playback.

### pause
```json
{
    "kind": "pause",
}
```

Pauses the playback. Does nothing when playback is stopped.

### resume
```json
{
    "kind": "resume",
}
```

Resumes the playback. Does nothing when playback is stopped.

### toggle
```json
{
    "kind": "toggle",
}
```

Toggles the playback. Does nothing when playback is stopped.

### stop
```json
{
    "kind": "stop",
}
```

Stops the playback.

### addqueue
```json
{
    "kind": "addqueue",
    "paths": array of strings
    "pos": integer (optional),
}
```

Adds songs from `paths` to the queue, starting at position `pos` (zero-indexed). Appends songs to the end if `pos` is not specified or invalid.

### play
```json
{
    "kind": "play",
    "id": integer,
}
```

Plays the song present in the queue with id equal to `id`.

### removequeue
```json
{
    "kind": "removequeue",
    "ids": array of integers,
}
```

Removes songs with ids `ids` from the queue.

### clearqueue
```json
{
    "kind": "clearqueue",
}
```

Clears the queue (removes all songs from it).

### next
```json
{
    "kind": "next",
}
```

Plays the next song (the next song is determined by the queue's playback mode). If there is no next song, stops the playback.

### previous
```json
{
    "kind": "previous",
}
```

Plays the previous song from the queue. If there is no previous song, stops the playback.

### modesingle
```json
{
    "kind": "modesingle",
}
```

Switches the queue into single mode: the playback stops after a song is finished.

### moderandom
```json
{
    "kind": "moderandom",
}
```

Switches the queue into random mode: the next song will be chosed from a pool of those enqueued songs that haven't been played yet. After the pool is exhausted, it's regenerated with every song from the queue.

### modesequential
```json
{
    "kind": "modesequential",
}
```

Switches the queue into sequential (the default) mode: songs are played one after another in order of their positions.

### state
```json
{
    "kind": "state",
}
```

Responds with information about the current state of Musing, in particular the response contains:
- the queue (as an array of entries, each entry containing the id and path of the song)
- the (zero-indexed) position in the queue of the current song (or `null` if playback is stopped)
- the base64-encoded cover art of the current song (if available)
- the playback state (playing/paused/stopped)
- the playback mode (single/random/sequential)
- the "gaplessness" of playback
- the volume
- the playback speed
- the timer (an object containing the duration of the current song as well as how many seconds elapsed since it started)
- the list of known playlists
- the list of audio devices (and whether they're disabled/enabled)

In order to prevent sending redundant data, the response is "delta-encoded" i.e. every client receives only the keys whose values have changed since the last time it requested `state`. The first response to any given client will always contain the full state.

Response:
```json
{
    "queue": array of objects,
    "current": integer or null,
    "cover_art": string,
    "playback_state": string,
    "playback_mode": string,
    "gapless": bool,
    "volume": integer,
    "speed": integer,
    "timer": object,
    "playlists": array of strings,
    "devices": array of objects,
}
```

Example request:
```json
{
    "kind": "state",
}
```
Example response:
```json
{
    "status": "ok",
    "queue": [{"id": 2, "path": "/some/song.mp3"}, {"id": 4, "path": "/another/song.m4a"}],
    "current": 1,
    "cover_art": "somebase64encodeddataxyz",
    "playback_state": "paused",
    "playback_mode": "random",
    "gapless": false,
    "volume": 60,
    "speed": 100,
    "timer": {"duration": 234, "elapsed": 100},
    "playlists": ["/playlist/dir/abc.m3u"],
    "devices": [{"device": "pipewire", "enabled": true}],
}
```

### disable
```json
{
    "kind": "disable",
    "device": string,
}
```

Disables the given audio device.

### enable
```json
{
    "kind": "enable",
    "device": string,
}
```

Enables the given audio device.

### addplaylist
```json
{
    "kind": "addplaylist",
    "playlist": string,
    "song": string,
}
```

Appends the `song` to `playlist` (an .m3u or .m3u8 file).

### listsongs
```json
{
    "kind": "listsongs",
    "playlist": string,
}
```

Returns an array containing paths of all songs in the `playlist` file. Paths are relative to the database's root directory.

Response:
```json
{
    "songs": array of strings
}
```

Example request:
```json
{
    "kind": "listsongs",
    "playlist": "/playlist/dir/foobar.m3u",
}
```
Example response:
```json
{
    "songs": ["song_one.mp3", "song_two.mp3"],
}
```

### load
```json
{
    "kind": "load",
    "playlist": string,
    "range": [integer, integer] (optional),
    "pos": integer (optional),
}
```

Loads the `playlist` to the queue. If `range = [i, j]` is provided, only songs from the `i`-th to the `j`-th one (zero-indexed) are loaded.
If `pos` is provided, then songs are inserted at position `pos` (also zero-indexed), otherwise they're appended to the end.
This command can succeed partially - all songs that were found in the database will be loaded, and the ones that weren't will be returned inside the `reason` key.
A status of `ok` will be returned only if all songs were found.

### removeplaylist
```json
{
    "kind": "removeplaylist",
    "playlist": string,
    "pos": integer,
}
```

Removes the song at position `pos` (zero-indexed) from the `playlist` file.

### save
```json
{
    "kind": "save",
    "path": string,
}
```

Saves the current queue as file at the given `path`. The created file conforms to the M3U format (one song per line).
Song paths are saved as relative to the database's root directory (which makes this operation cross-platform as relative paths are parsed as the same on UNIX and Windows).

## Supported tags
Musing supports the following tags (valid in all requests that require tag names):
- `album`
- `albumartist`
- `arranger`
- `artist`
- `bpm`
- `composer`
- `conductor`
- `date`
- `discnumber`
- `disctotal`
- `ensemble`
- `genre`
- `label`
- `language`
- `lyricist`
- `mood`
- `movementname`
- `movementnumber`
- `part`
- `parttotal`
- `performer`
- `producer`
- `script`
- `sortalbum`
- `sortalbumartist`
- `sortartist`
- `sortcomposer`
- `sorttracktitle`
- `tracknumber`
- `tracktitle`