ovi labs

Nightcore remake · verified · week of

Remaking a low-quality nightcore track in lossless: measure the speed and the pitch

To rebuild a sped-up edit of a song you need two numbers, how much faster and how much higher, and neither is what it first looks like. Measured, my copy lines up with the old file to 0.0009% in tempo and 0.14 cents in pitch.

Nightcore remake: three small Python scripts and ffmpeg · private scripts

I had an old nightcore edit as a low-quality MP3, and the original song as a lossless file. A nightcore edit is the song played faster, which also raises its pitch. So a clean copy needs only two numbers: how much faster, and how much higher. I wanted to measure them, not guess.

Why not divide the two lengths?

It is the obvious way to get the speed, and it was wrong. The old MP3 had 4.3 seconds of silence added at the end and its outro trimmed, two errors stacked. The lengths gave a speed of 1.104. The true one was 1.1113.

That gap is 0.66%. It sounds small, and it puts the beats 0.4 seconds apart after one minute.

A first speed, from the beat

Both files have a steady beat, so I measured the tempo of each and divided. The script follows how loud the track is over time, finds the spacing at which that pattern best repeats, and reads the beats per minute from it. That is good to about 0.3%, enough for a first copy.

The exact speed, from drift

Render the copy at that speed, then lay it next to the old file. If the speed is slightly off, the two slide apart at a steady rate, and that rate is the error.

gap between the two files
 +  |               .  '
    |        .  '
 0  |  .  '
    +---------------------- time
the slope is how wrong the speed is

The script compares ten-second pieces every thirty seconds and finds, for each, how far the copy has to slide to line up. A straight line through those gaps gives the correction: multiply the speed by one plus the slope, render again, and repeat until the slope is under 0.01%. One round is normally enough.

Pitch is a separate question

With the beats locked, the copy still sounded flat next to the old file. Speeding a song up by 1.1113 raises it by about 183 cents, hundredths of a semitone. The old edit was higher than that: its maker had added 82 cents on top.

To measure it, the script turns a stretch of each file into a spectrum on a pitch scale, in steps of 2 cents between 150 and 7,000 Hz. It removes the broad shape of the sound, including the dullness of the MP3, and slides one spectrum along the other until they line up. An ear hears 80 cents at once, even when every beat lines up.

The render

Pitch and tempo then have to be set apart. Resample the original by the total pitch, which makes it too fast, and slow it back to the measured tempo:

pitch  = tempo * 2^(cents/1200)
       = 1.1113 * 2^(82/1200) = 1.1652
atempo = tempo / pitch        = 0.9537

ffmpeg -i original.flac -af
 "asetrate=44100*1.1652,
  aresample=44100:resampler=soxr,
  atempo=0.9537"  out.flac

A stretch that small, under 5%, is clean on dance music.

How close it got

Measured again against the old MP3, the final file drifts by +0.0009% in tempo and sits 0.14 cents away in pitch. The speed came out very near ten ninths, which may be what the edit's maker typed. I kept the measured value.

I'm Ovi. I recompile old Windows games to run in a browser, build a virtual GPU for Windows XP, and write tools for my own machines, with AI since 2020. Projects and the latest log