Skip to content

Fix garbled duration in info() output for files of 10 minutes or longer - #497

Merged
bastibe merged 1 commit into
bastibe:masterfrom
sungbin1015:fix-info-duration-format
Oct 6, 2026
Merged

bastibe merged 1 commit into
bastibe:masterfrom
sungbin1015:fix-info-duration-format

Conversation

@sungbin1015

@sungbin1015 sungbin1015 commented Oct 6, 2026 •

Copy link
Copy Markdown
Contributor

repr(sf.info(file)) prints the duration in scientific notation as soon as the minutes or hours field reaches 10, and never zero-pads the seconds:

import numpy as np
import soundfile as sf

sf.write('x.wav', np.zeros(900 * 8, dtype='int16'), 8)   # 15 minutes
print(sf.info('x.wav'))
length current master with this PR
61 s 01:1.000 min 01:01.000 min
600 s 1e+01:0.000 min 10:00.000 min
900 s 2e+01:0.000 min 15:00.000 min
3599 s 6e+01:59.000 min 59:59.000 min
3661.5 s 1:01:1.500 h 1:01:01.500 h
11707.5 s 3:2e+01:7.500 h 3:15:07.500 h
36000 s 1e+01:00:0.000 h 10:00:00.000 h
119.9996 s 01:60.000 min 02:00.000 min

(soundfile 0.14.0 at 3503941, libsndfile 1.2.2, Python 3.11, Linux x86_64)

Cause

_SoundFileInfo._duration_str formats hours and minutes with .0g. g with precision 0 means one significant digit, so every value >= 10 is rounded to one digit and switches to exponent notation (format(15.0, '02.0g') == '2e+01'). The seconds use 05.3f, but d.ddd is already five characters wide, so the zero padding never applies; the width has to be 6.

The 4e+01 in the output quoted in #443 (6e+10:4e+01:3.719h) is this formatting; the frame count reported there is a separate matter.

Change

  • Use .0f / 02.0f for hours and minutes and 06.3f for seconds.
  • Round the duration to milliseconds before splitting it. Otherwise a value such as 119.9996 s is split into 1 min + 59.9996 s and then printed as 01:60.000 min.

The samples and s branches are unchanged, and so is the duration attribute itself.

Tests

info() had no test for the duration line. Added a parametrized test_info_duration covering all four branches; 6 of its 8 cases fail on master. python -m pytest: 339 passed. pyright soundfile.py reports the same two errors (line 188) before and after.

Related work

I found no issue or PR about the info() duration format (searched duration, _duration_str, e+01, info repr, scientific notation, open and closed).

🤖 Generated with Claude Code

`_SoundFileInfo._duration_str` formatted hours and minutes with `.0g`, which
keeps one significant digit and switches to exponent notation from 10 on
(15 minutes was shown as `2e+01:0.000 min`), and padded the seconds with
`05.3f`, which is too narrow to ever pad. Use `.0f` / `02.0f` / `06.3f`, and
round to milliseconds before splitting so that 119.9996 s is not shown as
`01:60.000 min`.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@bastibe
bastibe merged commit bffe99d into bastibe:master Oct 6, 2026
34 checks passed
@bastibe

bastibe commented Oct 6, 2026

Copy link
Copy Markdown
Owner

Thank you!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

The duration of the audio read by the soundfile library is incorrect

2 participants