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

Add audio sample count calculation function while maintain precision of the timestamp #9

Closed
drunkenQCat opened this issue Oct 13, 2024 · 3 comments · Fixed by #16
Closed
Assignees
Labels
enhancement New feature or request

Comments

@drunkenQCat
Copy link

I am a programmer working on sound, my work always needs to transfer SMPTE timecode to time stamp, and times them to 44100 or 48000.
我是音频技术,对SMPTE时间码转时间戳有需求。日常工作中,时间戳的结果要乘以44100或者48000。

But after running code below, the result appears so wired.
但是下面这段代码得出了预料之外的结果:

from dftt_timecode import DfttTimecode

a = DfttTimecode('01:00:00:13', 'smpte', fps=24, drop_frame=False, strict=True)
print(f"the timestamp of 01:00:00:13 is :  {a.timestamp}")
print(f"the sample count at 44100 is :  { a.timestamp * 44100 }")
print(f"the sample count at 48000 is :  { a.timestamp * 48000 }")

print("================================================")

a = DfttTimecode('01:00:00:14', 'smpte', fps=24, drop_frame=False, strict=True)
print(f"the timestamp of 01:00:00:14 is :  {a.timestamp}")
print(f"the sample count at 44100 is :  { a.timestamp * 44100 }")
print(f"the sample count at 48000 is :  { a.timestamp * 48000 }")
the timestamp of 01:00:00:13 is :  3600.54167
the sample count at 44100 is :  158783887.647
the sample count at 48000 is :  172826000.16
================================================
the timestamp of 01:00:00:14 is :  3600.58333
the sample count at 44100 is :  158785724.853
the sample count at 48000 is :  172827999.84

I think the problem may happens in data type tranferring or something else, but I haven't inspect the source code yet. Or maybe the maintainer has any consideration on it?
我觉得可能是数据类型转换时候出了问题,不过我还没有查到是源代码哪部分。或者这是作者设计上的考量吗?

@OwenYou
Copy link
Owner

OwenYou commented Oct 13, 2024

Hello! It's glad to know that this library is being used by the industry.

The problem you encountered is caused by multiplying a timestamp by a large value. timestamp is not designed to be used in such scenario, the value is already rounded before returning. So, instead, I recommend you to use precise_timestamp for this scenario.

a = DfttTimecode('01:00:00:13', 'smpte', fps=24, drop_frame=False, strict=True)
print(f"the timestamp of 01:00:00:13 is :  {a.timestamp}")
print(f"the sample count at 44100 is :  {float(a.precise_timestamp * 44100):.2f}")
print(f"the sample count at 48000 is :  {float(a.precise_timestamp * 48000):.2f}")

print("================================================")

a = DfttTimecode('01:00:00:14', 'smpte', fps=24, drop_frame=False, strict=True)
print(f"the timestamp of 01:00:00:14 is :  {a.timestamp}")
print(f"the sample count at 44100 is :  {float(a.precise_timestamp * 44100):.2f}")
print(f"the sample count at 48000 is :  {float(a.precise_timestamp * 48000):.2f}")
the timestamp of 01:00:00:13 is :  3600.54167
the sample count at 44100 is :  158783887.50
the sample count at 48000 is :  172826000.00
================================================
the timestamp of 01:00:00:14 is :  3600.58333
the sample count at 44100 is :  158785725.00
the sample count at 48000 is :  172828000.00

You can see that the code is giving the right result. 1 frame at 24 fps corresponds to 1837.5 samples at 44.1 kHz, or 2000 samples at 48 kHz.

But still, this is not quite an elegant way of use. So I am considering of packing this function(give out the audio sample counts of a tc instance under certain sample rate) into a class method.

Hope this resolves your problem!

Best regards.

@OwenYou
Copy link
Owner

OwenYou commented Oct 13, 2024

@WheheoHu We should consider add a class method to make such operations easier. Maybe the implementation should like:

# class method
get_audio_sample_counts(self, sample_rate):
    return math.floor(self.__precise_time*sample_rate)
# floor to avoid 'ghost' sample

@drunkenQCat
Copy link
Author

Thank you for your response! I will use this method in my project moving forward. I look forward to seeing this package being applied more widely in the future.

@OwenYou OwenYou added the enhancement New feature or request label Oct 14, 2024
@OwenYou OwenYou changed the title the precision of time stamp/ 时间戳的精度问题 Add audio sample count calculation function while maintain precision of the timestamp Oct 17, 2024
@WheheoHu WheheoHu self-assigned this Oct 18, 2024
This was referenced Oct 18, 2024
@OwenYou OwenYou reopened this Oct 19, 2024
This was referenced Oct 19, 2024
@WheheoHu WheheoHu linked a pull request Oct 19, 2024 that will close this issue
WheheoHu added a commit to WheheoHu/dftt_timecode that referenced this issue Oct 19, 2024
Remove unfinished timerange code
@WheheoHu WheheoHu removed a link to a pull request Oct 19, 2024
@WheheoHu WheheoHu linked a pull request Oct 19, 2024 that will close this issue
OwenYou added a commit that referenced this issue Oct 19, 2024
WheheoHu added a commit to WheheoHu/dftt_timecode that referenced this issue Jan 19, 2025
This reverts commit 6e72057.

# Conflicts:
#	dftt_timecode/error.py
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants