Pygame.mixer.sndarray.make_sound not working

222 views Asked by At

I am doing some simple DSP and playing the original and the filtered. I am using librosa to load the mp3 files in as arrays.

Here is the code of me loading the files in.

PATH = os.getcwd() + "/audio files"
    to_load_array = os.listdir(PATH)
    random_mp3 = to_load_array[randint(0, len(to_load_array) - 1)]
    random_mp3_path = "audio files/" + random_mp3

    data, sr = librosa.load(random_mp3_path, duration = MP3(random_mp3_path).info.length, mono = False, sr = 44100)

Else where in my program I have a GUI with a button that allows you to switch between playing the filtered version and and the unfiltered version. I am doing this by using different channels in pygame.mixer playing them both at the same time but alternating which one is being muted. I am using pygame.mixer because it allows for this simultaneous playing feature.

Currently I am filtering and then exporting the filtered file as an mp3 and then playing the mp3. This feels like too many I/O operations and I would like to speed up the program a little bit by playing the filtered and unfiltered arrays directly from within python.

I found the function pygame.mixer.sndarray.make_sound which is suppused to turn your array into a sound object that can be played on the mixer.play() function.

This is how I am using the function:

  def setSoundObjects(self, original, filtered):

        print('orignal data shape: ',original.shape)
        print('format (From get_init): ', pygame.mixer.get_init()[2])
        print("this is the original data: ", original)

        original = numpy.int16(original * (2 ** 15))
        filtered = numpy.int16(filtered * (2 ** 15))

        print("this is the data after numpy conversion: ", original)

        #set the sound objects
        print("Number of channels: ", pygame.mixer.get_num_channels())
        self.original = pygame.sndarray.make_sound(original)
        self.filtered = pygame.sndarray.make_sound(filtered)

However, whenever I try to use it I keep getting this error:

self.original = pygame.mixer.Sound(array = original)
ValueError: Array depth must match number of mixer channels

here is the output from what I am printing to the terminal to try and debug this:

format (From get_init):  2
this is the original data:  [[ 0.          0.          0.         ... -0.00801174 -0.00784447
  -0.01003712]
 [ 0.          0.          0.         ... -0.00695544 -0.00674583
  -0.00865676]]
this is the data after numpy conversion:  [[   0    0    0 ... -262 -257 -328]
 [   0    0    0 ... -227 -221 -283]]
Number of channels:  8

I was trying the numpy conversion because of this post but it didn't help.

Any help would be greatly appreciated thank you!

let me know if I should add anything else to help clarify what im doing. I did not include the entire code because the program is quite large already.

This question is different than this question because I am already changing the format of the array. I have tried this solution and it does not work in my code. I get the same error if I try and pass the size = 32 parameter into the init.

0

There are 0 answers