echonest / remix

Echo Nest Remix: The Internet Synthesizer
http://echonest.github.com/remix
414 stars 90 forks source link

AfromB example fails with casting errors with Numpy 1.10.1 #51

Open Quasimondo opened 8 years ago

Quasimondo commented 8 years ago

I am getting a casting error when trying to run the afromb example:

/home/ubuntu/anaconda/lib/python2.7/site-packages/echonest/remix/audio.py in mix(dataA, dataB, mix) 824 else: 825 newdata = AudioData(ndarray=dataB.data, sampleRate=dataB.sampleRate, numChannels=dataB.numChannels, defer=False) --> 826 newdata.data = 1 - float(mix) 827 newdata.data[:dataA.endindex] += dataA.data[:] \ float(mix) 828 return newdata

TypeError: Cannot cast ufunc multiply output from dtype('float64') to dtype('int16') with casting rule 'same_kind'

which looks very similar to the closed issue #13

I'm using an Anaconda Python 2.7 install which might contribute to the problem, though other examples work fine.

Quasimondo commented 8 years ago

I could fix it by changing the mix() function in audio.py. Not sure if this is very effective, but it works for me:

def mix(dataA, dataB, mix=0.5):
    """
    Mixes two `AudioData` objects. Assumes they have the same sample rate
    and number of channels.

    Mix takes a float 0-1 and determines the relative mix of two audios.
    i.e., mix=0.9 yields greater presence of dataA in the final mix.
    """
    if dataA.endindex > dataB.endindex:
        newdata = AudioData(ndarray=dataA.data, sampleRate=dataA.sampleRate, numChannels=dataA.numChannels, defer=False)
        tempdataA = newdata.data.astype(numpy.float64)
        tempdataA *= float(mix)
        tempdataB = dataB.data.astype(numpy.float64)[:]
        tempdataA[:dataB.endindex] += tempdataB * (1 - float(mix))
    else:
        newdata = AudioData(ndarray=dataB.data, sampleRate=dataB.sampleRate, numChannels=dataB.numChannels, defer=False)
        tempdataA = newdata.data.astype(numpy.float64)
        tempdataA *= 1 - float(mix)
        tempdataB = dataA.data.astype(numpy.float64)[:]
        tempdataA[:dataA.endindex] += tempdataB * float(mix)
    newdata.data = tempdataA.astype(numpy.int16,casting='unsafe')
    return newdata