echonest / remix

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

LocalAudioStream #38

Closed MikeiLL closed 10 years ago

MikeiLL commented 10 years ago

I'm working with P. Sobots ForeverFM module, which uses customized older version of Remix (and I think pyechonest).

Wondering if it would make sense to add the LocalAudioStream class to the audio module:

`class LocalAudioStream(AudioStream): """ Like a non-seekable LocalAudioFile with vastly better memory usage and performance. Takes a file-like object (and its kind, assumed to be MP3) and supports slicing and rendering. Attempting to read from a part of the input file that has already been read will throw an exception. """ def init(self, initializer, kind="mp3"): AudioStream.init(self, initializer)

    start = time.time()
    if hasattr(initializer, 'seek'):
        fobj = initializer
        fobj.seek(0)
    else:
        fobj = open(initializer, 'r')
    #   This looks like a lot of work, but is much more lighter
    #   on memory than reading the entire file in.
    md5 = hashlib.md5()
    while True:
        data = fobj.read(2 ^ 16)
        if not data:
            break
        md5.update(data)
    if not hasattr(initializer, 'seek'):
        fobj.close()
    track_md5 = md5.hexdigest()

    logging.getLogger(__name__).info("Fetching analysis...")
    try:
        tempanalysis = AudioAnalysis(str(track_md5))
    except EchoNestAPIError:
        tempanalysis = AudioAnalysis(initializer, kind)

    logging.getLogger(__name__).info("Fetched analysis in %ss",
                                     (time.time() - start))
    self.analysis = tempanalysis
    self.analysis.source = weakref.ref(self)

    class data(object):
        """
        Massive hack - certain operations are intrusive and check
        `.data.ndim`, so in this case, we fake it.
        """
        ndim = self.numChannels

    self.data = data

`

tkell commented 10 years ago

Ah, excellent, then you've already found the best way to deal with lots of audio.

We thought about adding Peter's streaming magic to the main repo, and decided against it - but for those people who want tons / streaming audio, it's a great place to start.