gerbera icon indicating copy to clipboard operation
gerbera copied to clipboard

Wrong sort order for audio files inside « Directories » virtual folder

Open Julien-Blanc-tgcm opened this issue 2 years ago • 3 comments

Gerbera version 1.9.2 (but from what i see, master should be affected as well).

I have a music collection arranged the following way :

<root directory>
 +-- Artist 1
        +---- Album1
                   +----- Song 1
                   +----- ....
                   +----- Song n
        +---- ....
        +---- Album n
                   +----- Song 1
                   +----- ...
 +-- Artist N
       .....

When adding this directory to the library, recursively, i can see under the « Audio/Directories » virtual folder the following hierarchy:

+-- Artist 1
        +---- Album1
                   +----- Song 

which is exactly what i want. The problem is that the songs are sorted alphabetically instead of being sorted by track number (even if the filename contains the track number). This means that the following folder:

01 - foo.mp3
02 - bar.mp3
03 - eeh.mp3
04 - zaz.mp3

gets displayed as

bar
eeh
foo
zaz

The order of the tracks is not preserved. It should be. The following patch:

--- a/src/database/sql_database.cc
+++ b/src/database/sql_database.cc
@@ -884,7 +884,10 @@ std::vector<std::shared_ptr<CdsObject>> SQLDatabase::browse(BrowseParam& param)
                 orderQb = sortParser.parse();
             }
             if (orderQb.empty()) {
-                orderQb = browseColumnMapper->mapQuoted(BrowseCol::dc_title);
+                orderQb = fmt::format("{},{},{}", 
+                                     browseColumnMapper->mapQuoted(BrowseCol::part_number),
+                                     browseColumnMapper->mapQuoted(BrowseCol::track_number),
+                                     browseColumnMapper->mapQuoted(BrowseCol::dc_title));
             }
             return orderQb;

fixes the issue for v1.9.2 (should be adapted for master, i'll make a PR if you think that's the right way to fix that). I have no clue if it breaks some other use cases (it solves mine).

Julien-Blanc-tgcm avatar Sep 22 '22 16:09 Julien-Blanc-tgcm

This should not be necessary if the virtual structure is correct.

Gerbera automatically sorts by part and track if the parent container is an album or playlist.

https://github.com/gerbera/gerbera/blob/3b93d362ec33f738868a4e896a2c9ee8e9d7a92a/src/upnp_cds.cc#L91-L92

If you are using subtype of musicAlbum it does not work in 1.9,2 but does in current head.

KarlStraussberger avatar Sep 22 '22 16:09 KarlStraussberger

Thanks for the reply. I'm using builtin layout. From the database content, i have the impression that two entries are created, one for the Albums virtual folder (in which everything is sorted correctly), and one for the Directories one (where wrong sort order occurs).

I'll try to reproduce with master, and keep you informed.

Julien-Blanc-tgcm avatar Sep 22 '22 16:09 Julien-Blanc-tgcm

You might be true with the pc directory because it reflects the file system and no virtual structure with metadata attached.

KarlStraussberger avatar Sep 22 '22 18:09 KarlStraussberger

With master the behaviour is different. I get under Directories

01 - bar
02 - eeh
03 - foo
04 - zaz

So, it's wrong as well. I get a pseudo track number, but items are still sorted alphabetically on the title, not by track id. I think it is wrong. It is ok to sort alphabetically without using the track number, but in that case this is the filename that should be the criteria, not the track title (this results in albums being completely scrambled). At least with a decently named audio collection it would work as expected. I'll check the database content later.

Julien-Blanc-tgcm avatar Sep 23 '22 06:09 Julien-Blanc-tgcm

Which directory are you talking about? Under "PC Directory" gerbera shows all files so we cannot assume that a track number is available. That's what the virtual layout is used for. If the problem happens under "Audio" please double check the parent folder - it should be of upnp class object.container.album.musicAlbum

KarlStraussberger avatar Sep 23 '22 07:09 KarlStraussberger

I'm talking about the V/Audio/Directories virtual folder.

sqlite> select * from mt_cds_object where id=20;
id|ref_id|parent_id|object_type|upnp_class|dc_title|location|location_hash|auxdata|update_id|mime_type|flags|part_number|track_number|service_id|last_modified|last_updated
20||12|1|object.container|Directories|V/Audio/Directories|3261617994||3||1|||||1663914250
sqlite> 
sqlite> select * from mt_cds_object where parent_id=20;
id|ref_id|parent_id|object_type|upnp_class|dc_title|location|location_hash|auxdata|update_id|mime
_type|flags|part_number|track_number|service_id|last_modified|last_updated
178||20|1|object.container|Dalmak|V/Audio/Directories/Dalmak|699656939||1||1|||||1663913718

So obviously the upnp class is missing for item 178. I guess this is why it is not sorted. Note that i am using the default builtin layout, without any special tweak. Under V/Audio/Albums the entry is correct:

172||15|1|object.container.album.musicAlbum|Dalmak|V/Audio/Albums/Dalmak|620805280||1||3|||||1663
913718

Julien-Blanc-tgcm avatar Sep 23 '22 10:09 Julien-Blanc-tgcm

Now I see your issue.

The responsible code is https://github.com/gerbera/gerbera/blob/c4c4e66e52bac37a2ef7a2729a9d69e78ab171db/src/content/layout/builtin_layout.cc#L413

We need to add upnp class to that call. This requires some thinking to work out a proper ruleset.

KarlStraussberger avatar Sep 23 '22 12:09 KarlStraussberger