0

I'm trying to arrange multiple PCM audio data into a specific sequence. the fact that it's audio data is just for context, the problem itself has nothing to do with audio/DSP.

my input is a varying set of files with varying lengths, and I'm trying to arrange the data sequentially into a new file and add padding after each segment where needed so that each input element is aligned to a grid which is integer-divisible by 120 units. In other words, I need to be able to address the beginning of each segment by choosing an offset between 0-119.

to illustrate the problem here is a trivial case example. two input files have the following byte lengths:

  200
+ 400
  ---
= 600 

in this case, there is no padding needed. the files can be arranged back to back, as they fit into the 120-grid as is. in the grid, the 200-file has a range from 0-40 (40 units), the 400 file has a range from 40-120 (80 units).

this becomes trickier if any of the files do not fit into the grid.

  199
+ 398
  ---
= 597 

intuitively, it's easy to see that the 199-byte file needs 1 byte of padding at the end so that its length becomes 200, and the 398-byte file needs 2 bytes to become 400 bytes. We then have a nice 1:2 ratio between the 2 files, which in the 120-grid translates to 40 and 80 units.

now, I'm trying to find an algorithm which can do this for any number of input files from 1-120, where each file can have arbitrary non-zero length.

maybe there is an existing algorithm which does just that, but I'm finding it difficult to find descriptive keywords for the problem.

I've tried to solve this naively, but somehow I fail to grok the problem fully. Basically I need to grow the individual files so that their sizes are multiples of the smallest common denominator of the sum of their lengths - which to me is kind of a chicken/egg problem. If I grow the files so their ratios fit together, I also grow the sum of their lengths and I don't understand how to check both against the 120-grid...

edit: ok I think I got it: https://gist.github.com/jpenca/b033122fcb2300c5e9e4 not sure how to prove correctness, but trying this with varying inputs seems to work ok.

spinalwrap
  • 683
  • 1
  • 4
  • 17

0 Answers0