I have the code

import sound

def rem_vocals(snd):
    '''Return a copy of the original sound with vocals removed. The original sound is unmodified. The number of samples in the copied sound is the same as the original file.'''
    new_song=sound.copy(snd)
    for samp in new_song:
        left=(sound.get_left(samp))
        right=(sound.get_right(samp))
        result=(left-right)/2.0
        Left=sound.set_left(samp, int (result))
        Right=sound.set_right(samp, int (result))
        Left1=(sound.get_left(samp))
        Right1=(sound.get_right(samp))
    return new_song

if __name__ == '__main__':
    song=sound.load_sound('love.wav')

Question: How do I play the copy of my sound. sound.play(song) plays the original song, not the modified one. I then tried sound.play(new_song) which invokes an error. So what do I put inside the () to play the modified song? Thanks.

Dani AI

Generated

Short answer: the sound that gets played must be the object returned by rem_vocals, not the original song. and were on the right track and confirmed that calling the function and playing its returned object fixes the immediate playback issue.

Common reasons sound.play(...) on the copy can still raise an error:

  • the local variable new_song exists only inside rem_vocals unless the function return is assigned; calling sound.play(new_song) without first doing new_song = rem_vocals(song) will fail.
  • the returned value might not be the expected sound object (different type or structure) — a quick type/length check helps.
  • assignments like Left = sound.set_left(...) are often incorrect: many set_ APIs return None and mutate in place, so storing their return and later using it can cause confusion.
  • numeric issues: converting floats to int without clamping can overflow the sample range and produce undefined behavior in playback.

Practical, non-invasive fixes and checks:

  • assign the result explicitly and inspect it (new_song = rem_vocals(song) ; check types/lengths with prints) before playing.
  • remove useless assignments from inside rem_vocals (don’t assign the return of set_left/set_right), avoid rereading values immediately after setting them, and use int(round(...)) plus clamping to the valid sample range (e.g., -32768..32767) to avoid overflow.
  • if playback still errors, save/export the modified sound (if the module supports it) and play the file with an external player to isolate whether the problem is the sound object or the sound.play call.
    These steps clarify scope, object type, and numeric safety and will resolve most issues beyond the simple “pass the returned object to play” fix.

Recommended Answers

All 4 Replies

rem_vocals(sound)

I don't mean that. I already called the function. I mean how do I play the copied song using the sound.play function?

Should be like pyTony says call sound.play(rem_vocals(sound)) because your rem_vocals function is returning the new__song. But are you sure your error is not from import sound. I don't see any module that exists with that name unless you have made it yourself.

commented: gj +8

Ok, thanks for the clarifying! sound.play(rem_vocals(song)) works! I thought pyTony meant to call the function! No, I did not create the sound module. If you go to python 2.7 and key in import sound and then dir(sound) in the python shell, you will see a huge list of info.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.