1

I'm writing a program that sequences music out of a bunch of stems differently each time. I now have a basic GUI with buttons that trigger resequencing, playing and bouncing of the track.

So, on execution, the program triggers the resequencing function once, and returns the output value ready for playback. This can be played using the play function, but to receive output within the function, the initial function has to be retriggered, which is not right.

I do not want to retrigger the sequencing function each time the user wants to replay the track, I just need to access the initial return data. I cannot assign the returned data outside the resequence function, as it would only be saved once, and only the first sequence would be available to play.

The below code example is what I have, and it doesnt work for me.

what I need is a way to save a variable each time resequencing occurs, and make it accessible to the play function. Play should not retrigger resequencing just in order to get the value.

:::

Resequence():

blah blah, sequencing.

return output

Play():

output=Resequence()   # the value shouldnt change on each play. 

play(output)

:::

Output is an Audio segment from PyDub. I tried saving it in a text file, which obviously did not work.

Please help :(

Sebastian
  • 11
  • 1
  • 2
    So make `output` global and check if it is already containing a value before assigning (or if there is no way to do this - add a global flag indicating it). – Eugene Sh. Jul 16 '15 at 14:15
  • You might be able to use the `pickle` module to save the output. If not, perhaps `pydub` has some facilities to save and load it. – martineau Jul 16 '15 at 14:15
  • 1
    Please provide more code in order to give you a concrete answer. Thank you. – Mihai Hangiu Jul 16 '15 at 14:16
  • Put your code in a class, e.g. `class Player: ...` and make `output` an instance variable (-> `self.output = ...`). Access it via `try: ... except AttributeError: ...`. – a_guest Jul 16 '15 at 14:18
  • Before making it a global variable, consider that [_Global Variables Are Bad_](http://c2.com/cgi/wiki?GlobalVariablesAreBad). – martineau Jul 16 '15 at 14:50

1 Answers1

1

If Resequence is a function and not a class method then you probably need to use a global variable to store the required sequence.Something like this:

seq = None
def Resequence():
    global seq
    if seq != None:
        return seq
    #now do your blah, blah, blah
    seq = my_generated_sequence
    return seq

However I feel that you need to make Resequence part of an object, to allow multiple calls for different files each time, etc.

class Player(object):
     def __init__(self):
         self._seq = None
     def Resequence(self):
        if self._seq != None:
           #same logic here...
Arthur.V
  • 676
  • 1
  • 8
  • 22