@@ -124,7 +124,8 @@ class CrossfadeData:
124124
125125 data : bytes
126126 fade_in_size : int
127- pcm_format : AudioFormat
127+ pcm_format : AudioFormat # Format of the 'data' bytes (current/previous track's format)
128+ fade_in_pcm_format : AudioFormat # Format for 'fade_in_size' (next track's format)
128129 queue_item_id : str
129130
130131
@@ -1453,8 +1454,9 @@ async def get_queue_item_stream_with_smartfade(
14531454
14541455 if crossfade_data :
14551456 # Calculate discard amount in seconds (format-independent)
1457+ # Use fade_in_pcm_format because fade_in_size is in the next track's original format
14561458 fade_in_duration_seconds = (
1457- crossfade_data .fade_in_size / crossfade_data .pcm_format .pcm_sample_size
1459+ crossfade_data .fade_in_size / crossfade_data .fade_in_pcm_format .pcm_sample_size
14581460 )
14591461 discard_seconds = int (fade_in_duration_seconds ) - 1
14601462 # Calculate discard amounts in CURRENT track's format
@@ -1620,7 +1622,7 @@ async def get_queue_item_stream_with_smartfade(
16201622 # edge case: pcm format mismatch, we need to resample the next track's
16211623 # beginning part before crossfading
16221624 self .logger .debug (
1623- "Resampling next track from %s to %s for queue %s" ,
1625+ "Resampling next track's crossfade from %s to %s for queue %s" ,
16241626 next_queue_item_pcm_format .sample_rate ,
16251627 pcm_format .sample_rate ,
16261628 queue .display_name ,
@@ -1662,15 +1664,17 @@ async def get_queue_item_stream_with_smartfade(
16621664 for _chunk in divide_chunks (crossfade_first , pcm_format .pcm_sample_size ):
16631665 yield _chunk
16641666 # store the other half for the next track
1665- # IMPORTANT: Use original buffer size (in next track's format) for fade_in_size
1666- # because the next track will stream in its native format and needs to know
1667- # how many bytes to discard in that format.
1668- # However, crossfade_second data is in current track's format (pcm_format)
1667+ # IMPORTANT: crossfade_second data is in CURRENT track's format (pcm_format)
16691668 # because it was created from the resampled buffer used for mixing.
1669+ # BUT fade_in_size represents bytes in NEXT track's original format
1670+ # (next_queue_item_pcm_format) because that's how much of the next track
1671+ # was consumed during the crossfade. We need both formats to correctly
1672+ # handle the crossfade data when the next track starts.
16701673 self ._crossfade_data [queue_item .queue_id ] = CrossfadeData (
16711674 data = crossfade_second ,
16721675 fade_in_size = original_buffer_size ,
1673- pcm_format = pcm_format ,
1676+ pcm_format = pcm_format , # Format of the data (current track)
1677+ fade_in_pcm_format = next_queue_item_pcm_format , # Format for fade_in_size
16741678 queue_item_id = next_queue_item .queue_item_id ,
16751679 )
16761680 except Exception as err :
0 commit comments