Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Adding Tracks to Queue from Search Results #1607

Open
cdlenfert opened this issue May 10, 2023 · 6 comments
Open

Adding Tracks to Queue from Search Results #1607

cdlenfert opened this issue May 10, 2023 · 6 comments
Labels

Comments

@cdlenfert
Copy link
Contributor

This one is a little hard to explain but I think these are the steps to replicate the issue I'm seeing:

  • browse to a playlist (Spotify in this case) and click the shuffle button to start playback and build a queue
  • browse to another playlist (again Spotify) and on a single track within the playlist click the vertical elipsis and choose "Add Next" from the options in the modal
    • the track is added as the next track in the queue as expected
  • now use the search to find a track (on Spotify for consistency sake)
  • click the vertical elipsis for the track and choose "Add Next"
    • this track is added somewhere randomly in the queue (i.e. the queue is apparently re-shuffled) and a lot of previously unplayed tracks in the que show as previously played (greyed out)

I have no problem when adding (next) tracks to the initial shuffled queue when browsing these tracks in my library, but when I do the same from search results, the shuffled queue gets reshuffled and the searched track is not next in the queue either.

@ejurgensen
Copy link
Member

Thanks for reporting this @cdlenfert. As I recall, queue handling especially when shuffled is surprisingly complicated, so I have a feeling it won't be an easy fix.

@ejurgensen ejurgensen added the bug label May 16, 2023
@cdlenfert
Copy link
Contributor Author

My pleasure, and also sorry to be a pain.

It may not be of any help, but I use an iOS shortcut to search for and add tracks into the current position in the queue. It honors (as far as I know) the shuffled queue while doing so. This is all via the API.

https://www.icloud.com/shortcuts/69fa3815cf7d4b1ca1a5485a9785e748

@hacketiwack
Copy link
Collaborator

I could reproduce this behaviour from the Spotify search page.
Adding a song from the library search, does not provoke this issue.
The same method on the API is called for library tracks and Spotify tracks, however the behaviour is different.

This call works:

axios.post('./api/queue/items/add?uris=library:track:335664&position=7')

This one doesn't:

axios.post('./api/queue/items/add?uris=spotify:track:7ESkZSRagu9qKszQxq7Qsl&position=7')

How differently are treated Spotify tracks behind the scene?

@ejurgensen
Copy link
Member

I think the queue handling is agnostic to source type. What does the debug log say for the one that doesn't work?

@hacketiwack
Copy link
Collaborator

hacketiwack commented Jun 27, 2023

Below, is an extract from the logs.
First when I add one track from the library and then from Spotify.
It seems to trigger a reshuffle, but, IMHO, adding a track to an existing queue (even in shuffle mode) shouldn't trigger a reshuffle.

[2023-06-27 12:55:50] [DEBUG]      web: JSON api request: '/api/queue/items/add?uris=library:track:293836&position=1'
[2023-06-27 12:55:50] [DEBUG]      web: Add tracks starting at position '1
[2023-06-27 12:55:50] [DEBUG]   player: Player status: stopped
[2023-06-27 12:55:50] [DEBUG]       db: Running query 'BEGIN TRANSACTION;'
[2023-06-27 12:55:50] [DEBUG]       db: Running query 'SELECT value FROM admin a WHERE a.key = 'queue_version';'
[2023-06-27 12:55:50] [DEBUG]       db: Running query 'SELECT COUNT(*) FROM queue;'
[2023-06-27 12:55:50] [DEBUG]       db: Running query 'SELECT COUNT(*) FROM files f WHERE f.disabled = 0 AND (f.id = 293836);'
[2023-06-27 12:55:50] [DEBUG]       db: Starting query 'SELECT f.* FROM files f WHERE f.disabled = 0 AND (f.id = 293836)  ORDER BY f.album_sort, f.disc, f.track ;'
[2023-06-27 12:55:50] [DEBUG]       db: Player queue query returned 1 items
[2023-06-27 12:55:50] [DEBUG]       db: Running query 'UPDATE queue SET pos = pos + 1, queue_version = 7869 WHERE pos >= 1;'
[2023-06-27 12:55:50] [DEBUG]       db: Running query 'UPDATE queue SET shuffle_pos = shuffle_pos + 1, queue_version = 7869 WHERE shuffle_pos >= 1;'
[2023-06-27 12:55:50] [DEBUG]       db: Running query 'INSERT INTO queue (file_id, pos, shuffle_pos, data_kind, media_kind, song_length, path, virtual_path, title, artist, album_artist, album, genre, songalbumid, time_modified, artist_sort, album_sort, album_artist_sort, year, track, disc, artwork_url, queue_version, composer, songartistid, type, bitrate, samplerate, channels) VALUES (293836, 1, 1, 0, 1, 231262, '/audio/Music/a/e/ae50ea35-1a2d-40bb-8de4-2083bfd8a32e/dc550b37-47e5-373d-858e-b08e0cbe6e88.mp3', '/file:/audio/Music/a/e/ae50ea35-1a2d-40bb-8de4-2083bfd8a32e/dc550b37-47e5-373d-858e-b08e0cbe6e88.mp3', 'Attinde (Sardinia)', 'Càlic', 'Various Artists', 'Putumayo Presents: Italian Musical Odyssey', 'Latin', 4789712423190246650, 1654974156, 'Càlic', 'Putumayo Presents: Italian Musical Odyssey', 'Various Artists', 2000, 7, 1, NULL, 7869, NULL, 8395563705718003786, 'mp3', 131, 44100, 2);'
[2023-06-27 12:55:50] [DEBUG]       db: Added (pos=1 shuffle_pos=1 reshuffle=1 req position=1) song id 293836 (Attinde (Sardinia)) to queue with item id 1383771
[2023-06-27 12:55:50] [DEBUG]       db: End of query results
[2023-06-27 12:56:07] [DEBUG]      web: JSON api request: '/api/queue/items/add?uris=spotify:track:6XBaTMiZa77Du2XEl1RNaa&position=1'
[2023-06-27 12:56:07] [DEBUG]      web: Add tracks starting at position '1
[2023-06-27 12:56:07] [DEBUG]   player: Player status: stopped
[2023-06-27 12:56:07] [DEBUG]      lib: Add items for path 'spotify:track:6XBaTMiZa77Du2XEl1RNaa' to the queue
[2023-06-27 12:56:07] [DEBUG]  spotify: Spotify token still valid
[2023-06-27 12:56:07] [DEBUG]  spotify: Request Spotify API endpoint: 'https://api.spotify.com/v1/tracks/6XBaTMiZa77Du2XEl1RNaa')
[2023-06-27 12:56:07] [ INFO]     http: Making request for https://api.spotify.com/v1/tracks/6XBaTMiZa77Du2XEl1RNaa
[2023-06-27 12:56:07] [DEBUG]  spotify: Spotify API endpoint request: 'https://api.spotify.com/v1/tracks/6XBaTMiZa77Du2XEl1RNaa'
[2023-06-27 12:56:07] [DEBUG]  spotify: Got track: 'Les sardines' (spotify:track:6XBaTMiZa77Du2XEl1RNaa) 
[2023-06-27 12:56:07] [DEBUG]       db: Running query 'BEGIN TRANSACTION;'
[2023-06-27 12:56:07] [DEBUG]       db: Running query 'SELECT value FROM admin a WHERE a.key = 'queue_version';'
[2023-06-27 12:56:07] [DEBUG]       db: Running query 'SELECT COUNT(*) FROM queue;'
[2023-06-27 12:56:07] [DEBUG]       db: Running query 'INSERT INTO queue (file_id, pos, shuffle_pos, data_kind, media_kind, song_length, path, virtual_path, title, artist, album_artist, album, genre, songalbumid, time_modified, artist_sort, album_sort, album_artist_sort, year, track, disc, artwork_url, queue_version, composer, songartistid, type, bitrate, samplerate, channels) VALUES (9999999, 1, 401, 2, 1, 233866, 'spotify:track:6XBaTMiZa77Du2XEl1RNaa', '/spotify:track:6XBaTMiZa77Du2XEl1RNaa', 'Les sardines', 'Patrick Sébastien', 'Patrick Sébastien', 'Pochette Surprise', 'Unknown genre', 0, 0, 'Patrick Sébastien', 'Pochette Surprise', 'Patrick Sébastien', 0, 2, 1, 'https://i.scdn.co/image/ab67616d00001e0296bb90149b37c96791752e45', 7870, NULL, 0, NULL, 0, 0, 0);'
[2023-06-27 12:56:07] [DEBUG]       db: Running query 'UPDATE queue SET pos = pos + 1, queue_version = 7870 WHERE pos >= 1 AND queue_version < 7870;'
[2023-06-27 12:56:07] [DEBUG]       db: Reshuffle queue after item with item-id: 0
[2023-06-27 12:56:07] [DEBUG]       db: Running query 'UPDATE queue SET shuffle_pos = pos, queue_version = 7870;'
[2023-06-27 12:56:07] [DEBUG]       db: Running query 'SELECT COUNT(*) FROM queue;'
[2023-06-27 12:56:07] [DEBUG]       db: Reshuffle 402 items off 402 total items, starting from pos 0
[2023-06-27 12:56:07] [DEBUG]       db: Starting enum 'SELECT * FROM queue f WHERE pos >= 0 ORDER BY pos;'
[2023-06-27 12:56:07] [DEBUG]       db: Running query 'UPDATE queue SET shuffle_pos = 235 where id = 1383371;'
....
[2023-06-27 12:56:08] [DEBUG]       db: Running query 'UPDATE queue SET shuffle_pos = 276 where id = 1383770;'
[2023-06-27 12:56:08] [DEBUG]       db: End of queue enum results
[2023-06-27 12:56:08] [DEBUG]       db: Running query 'INSERT OR REPLACE INTO admin (key, value) VALUES ('queue_version', '7870');'
[2023-06-27 12:56:08] [DEBUG]       db: Running query 'END TRANSACTION;'
[2023-06-27 12:56:08] [DEBUG]      mpd: Asynchronous listener callback called with event type 2.
[2023-06-27 12:56:08] [DEBUG]      mpd: Notify clients waiting for idle results: 2
[2023-06-27 12:56:08] [DEBUG]      web: notify callback reason: 71
[2023-06-27 12:56:08] [DEBUG]      lib: Items for path 'spotify:track:6XBaTMiZa77Du2XEl1RNaa' from library source 'spotify' added to the queue

@ejurgensen
Copy link
Member

I looked into this, and it looks like the reason Spotify is different is actually that queue_tracks_add_byuris() distinguishes between library items and non-library items. When it's a library item db_queue_add_by_query() will be called, and even though it is called with reshuffle = true there is this logic.

Non library items are added with library_queue_item_add() which doesn't have that logic.

I need to think about how to fix this. Copying the logic to the non-library function doesn't feel super clean.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

3 participants