Skip to content

Best way to deepcopy a track #60

Open
@tleyden

Description

@tleyden

I'm trying to get a deep copy of a track via:

copy.deepcopy(track)

but I'm getting this error:

  File "/Users/tleyden/Development/climate_music/src/transformer.py", line 52, in transform
    track_copy = copy.deepcopy(track)
  File "/Users/tleyden/DevLibraries/anaconda/lib/python2.7/copy.py", line 190, in deepcopy
    y = _reconstruct(x, rv, 1, memo)
  File "/Users/tleyden/DevLibraries/anaconda/lib/python2.7/copy.py", line 351, in _reconstruct
    item = deepcopy(item, memo)
  File "/Users/tleyden/DevLibraries/anaconda/lib/python2.7/copy.py", line 190, in deepcopy
    y = _reconstruct(x, rv, 1, memo)
  File "/Users/tleyden/DevLibraries/anaconda/lib/python2.7/copy.py", line 346, in _reconstruct
    setattr(y, key, value)
  File "/Users/tleyden/DevLibraries/anaconda/lib/python2.7/site-packages/midi/events.py", line 125, in set_velocity
    self.data[1] = val
AttributeError: data

As an alternative, I'm writing my own kludge and just propagating the events I care about:

def copy_track(track):
    """
    copy.deepcopy() didn't work, so I hand rolled this as a workaround
    """
    track_copy = midi.Track()
    for event in track:
        if isinstance(event, midi.NoteOnEvent):
            on = midi.NoteOnEvent(tick=event.tick, velocity=event.velocity, pitch=event.pitch)
            track_copy.append(on)
        if isinstance(event, midi.NoteOffEvent):
            off = midi.NoteOffEvent(tick=event.tick, velocity=event.velocity, pitch=event.pitch)
            track_copy.append(off)
     return track_copy

If it's not feasible to make copy.deepcopy() work, is there a more elegant way of rolling my own?

Metadata

Metadata

Assignees

No one assigned

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions