How to split measures into beats and subdivide them, respecting the old notes, in a musical notation app

I have 4 * 8 dots. I want to logs this like this:

// empty()      // ................................

I want to divide this into 4 parts and show an “4” every eight dot.

// init()          // 4.......4......4.......4.......

I want to divide the first part into two and place a “2” every fourth dot.

// add(0, 2)       // 2...2...4......4.......4.......

In other words I placed a “2” at 0th dot which was “4” previously. This lasts for only 4 dots, so the
remaining 4 dots are left with another “2”.

How I think of this is more like placing an “8” at place 4:

// add(4, 8)       // 2...8..............2...4.......

So the number at the beginning of dots is measure of how long a dot lasts, and I can place a number anywhere I feel like. But I need to respect the old dots, and put new dots to keep them numbered.
In the last example I had to place an “8” at place 4 so I removed “2” “4” and “4” and replaced it with an “8” and also added a “2” to make up for the remaining space.

Final example we place a “6” at the 6th place.

// add(6, 6)       // 2...116..........112...4.......

This overrode “8” and replaced it with “6” and a “2” and a bunch of “1”‘s.

Overall what I want:

/*
  // time signature 4/4
  // method call  // logs
  init()          // 4.......4......4.......4.......
  add(0, 2)       // 2...2...4......4.......4.......
  add(4, 8)       // 2...8..............2...4.......
  add(6, 6)       // 2...116..........112...4.......
*/

Basically I want an abstraction for putting musical notes and querying them.

I’ve tried two approaches but they are just hardcore messy calculations that didn’t work. I need some neat functional
approach easy to grok and handle.