Example #1
0
def cacheMetadata(
        corpusNames=('local', 'core', 'virtual'),
        useMultiprocessing=True,
):
    '''
    Cache metadata from corpuses in `corpusNames` as local cache files:

    Call as ``metadata.cacheMetadata()``

    '''
    from music21 import corpus
    from music21 import metadata

    if not common.isListLike(corpusNames):
        corpusNames = (corpusNames, )

    timer = common.Timer()
    timer.start()

    # store list of file paths that caused an error
    failingFilePaths = []

    # the core cache is based on local files stored in music21
    # virtual is on-line
    for corpusName in corpusNames:
        if corpusName == 'core':
            metadataBundle = metadata.MetadataBundle.fromCoreCorpus()
            paths = corpus.getCorePaths()
            useCorpus = True
        elif corpusName == 'local':
            metadataBundle = metadata.MetadataBundle.fromLocalCorpus()
            paths = corpus.getLocalPaths()
            useCorpus = False
        elif corpusName == 'virtual':
            metadataBundle = metadata.MetadataBundle.fromVirtualCorpus()
            paths = corpus.getVirtualPaths()
            useCorpus = False
        else:
            message = 'invalid corpus name provided: {0!r}'.format(corpusName)
            raise MetadataCacheException(message)
        message = 'metadata cache: starting processing of paths: {0}'.format(
            len(paths))
        environLocal.printDebug(message)
        failingFilePaths += metadataBundle.addFromPaths(
            paths,
            useCorpus=useCorpus,
            useMultiprocessing=useMultiprocessing,
        )
        message = 'cache: writing time: {0} md items: {1}'.format(
            timer, len(metadataBundle))
        environLocal.printDebug(message)
        del metadataBundle
    message = 'cache: final writing time: {0} seconds'.format(timer)
    environLocal.printDebug(message)
    for failingFilePath in failingFilePaths:
        message = 'path failed to parse: {0}'.format(failingFilePath)
        environLocal.printDebug(message)
Example #2
0
def cacheMetadata(
    corpusNames=('local', 'core', 'virtual'),
    useMultiprocessing=True,
    ):
    '''
    Cache metadata from corpuses in `corpusNames` as local cache files:

    Call as ``metadata.cacheMetadata()``

    '''
    from music21 import corpus
    from music21 import metadata

    if not common.isListLike(corpusNames):
        corpusNames = (corpusNames,)

    timer = common.Timer()
    timer.start()

    # store list of file paths that caused an error
    failingFilePaths = []

    # the core cache is based on local files stored in music21
    # virtual is on-line
    for corpusName in corpusNames:
        if corpusName == 'core':
            metadataBundle = metadata.MetadataBundle.fromCoreCorpus()
            paths = corpus.getCorePaths()
            useCorpus = True
        elif corpusName == 'local':
            metadataBundle = metadata.MetadataBundle.fromLocalCorpus()
            paths = corpus.getLocalPaths()
            useCorpus = False
        elif corpusName == 'virtual':
            metadataBundle = metadata.MetadataBundle.fromVirtualCorpus()
            paths = corpus.getVirtualPaths()
            useCorpus = False
        else:
            message = 'invalid corpus name provided: {0!r}'.format(corpusName)
            raise MetadataCacheException(message)
        message = 'metadata cache: starting processing of paths: {0}'.format(
                len(paths))
        environLocal.printDebug(message)
        failingFilePaths += metadataBundle.addFromPaths(
            paths,
            useCorpus=useCorpus,
            useMultiprocessing=useMultiprocessing,
            )
        message = 'cache: writing time: {0} md items: {1}'.format(
            timer, len(metadataBundle))
        environLocal.printDebug(message)
        del metadataBundle
    message = 'cache: final writing time: {0} seconds'.format(timer)
    environLocal.printDebug(message)
    for failingFilePath in failingFilePaths:
        message = 'path failed to parse: {0}'.format(failingFilePath)
        environLocal.printDebug(message)
Example #3
0
def main():
    parser = get_cmd_line_parser(description=__doc__)
    ParserArguments.filename(parser)
    ParserArguments.tempo(parser)
    ParserArguments.framerate(parser)
    ParserArguments.set_defaults(parser)
    ParserArguments.best(parser)
    args = parser.parse_args()
    defaults.framerate = args.framerate

    print('Generating Signal:')
    sig_gen = Generator()
    song = Waveform([])
    qnl = quarter_note_length(args.tempo)

    # work = corpus.parse(numpy.random.choice(corpus.getComposer('bach')))
    work = corpus.parse(numpy.random.choice(corpus.getCorePaths()))
    notes = work.flat.notes
    if args.best:
        audify_to_file(notes, args.tempo, args.filename, args.verbose)
        return 0

    note_count = len(notes)
    try:
        for count, note in enumerate(notes):
            print('{}/{}: {} [{}]: {} {}'.format(count, note_count,
                                                 note.offset,
                                                 note.duration.quarterLength,
                                                 note.pitch,
                                                 note.pitch.frequency))
            note_length = qnl * note.quarterLength
            start = seconds_to_frame(qnl * note.offset)
            print('  inserting {} seconds into frame {}'.format(
                note_length, start))
            song = song.insert(
                start,
                sig_gen.sin_constant(note.pitch.frequency, length=note_length))
    except KeyboardInterrupt:
        print('Stopping song generating here...')

    print('Writing Song {} to file {}...'.format(work.corpusFilepath,
                                                 args.filename))
    with wav_file_context(args.filename) as fout:
        fout.write_frames(song.frames)

    return 0
Example #4
0
def main():
    parser = get_cmd_line_parser(description=__doc__)
    ParserArguments.filename(parser)
    ParserArguments.tempo(parser)
    ParserArguments.framerate(parser)
    ParserArguments.set_defaults(parser)
    args = parser.parse_args()
    defaults.framerate = args.framerate

    print('Generating Signal:')
    work = corpus.parse(numpy.random.choice(corpus.getCorePaths()))
    notes = work.flat.notes
    waveform = audify(notes, args.tempo, args.verbose)

    print('Writing Song {} to file {}...'.format(work.corpusFilepath,
                                                 args.filename))
    with wav_file_context(args.filename) as fout:
        fout.write_frames(waveform.frames)

    return 0
Example #5
0
def main():
    parser = get_cmd_line_parser(description=__doc__)
    ParserArguments.filename(parser)
    ParserArguments.tempo(parser)
    ParserArguments.framerate(parser)
    ParserArguments.set_defaults(parser)
    args = parser.parse_args()
    defaults.framerate = args.framerate

    print('Generating Signal:')
    work = corpus.parse(numpy.random.choice(corpus.getCorePaths()))
    notes = work.flat.notes
    waveform = audify(notes, args.tempo, args.verbose)

    print('Writing Song {} to file {}...'.format(
        work.corpusFilepath, args.filename))
    with wav_file_context(args.filename) as fout:
        fout.write_frames(waveform.frames)

    return 0
def cacheCoreMetadataMultiprocess(ipythonMod = None, stopAfter = None): 
    '''The core cache is all locally-stored corpus files. 
    '''
    from music21 import corpus, metadata

    t = common.Timer()
    t.start()

    # store list of file paths that caused an error
    #fpError = []

    mdb = metadata.MetadataBundle('core')

    pathsFull = corpus.getCorePaths()
    pathsShort = []
    lenCorpusPath = len(common.getCorpusFilePath())
    
    for i,p in enumerate(pathsFull):
        pathsShort.append(p[lenCorpusPath:])
        if stopAfter is not None and i >= stopAfter:
            break
    
    environLocal.warn([
            'metadata cache: starting processing of paths:', len(pathsShort)])
    
    #mdb.addFromPaths(paths[-3:])
    # returns any paths that failed to load
    for i in range(0, len(pathsShort), 100):
        maxI = min(i+100, len(pathsShort))
        pathsChunk = pathsShort[i:maxI]
        environLocal.warn('Starting multiprocessing with chunk %d, first is %s' % (i, pathsChunk[0]))
        allKeys = ipythonMod.map_async(cacheCoreMetadataMultiprocessHelper, pathsChunk)
        for thisKey in allKeys:
            for thisSubKey in thisKey:
                    mdb.storage[thisSubKey[0]] = thisSubKey[1]
    
    #print mdb.storage
    mdb.write() # will use a default file path based on domain

    environLocal.warn(['cache: writing time:', t, 'md items:', len(mdb.storage)])
    del mdb