Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

AfromB example fails with casting errors with Numpy 1.10.1 #51

Open
Quasimondo opened this issue Feb 8, 2016 · 1 comment
Open

AfromB example fails with casting errors with Numpy 1.10.1 #51

Quasimondo opened this issue Feb 8, 2016 · 1 comment

Comments

@Quasimondo
Copy link

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
Copy link
Author

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant