コード例 #1
0
ファイル: audioimages.py プロジェクト: imclab/pycompmusic
    def run(self, fname):
        baseFname, ext = os.path.splitext(os.path.basename(fname))

        wavfname, created = util.docserver_get_wav_filename(self.musicbrainz_id)

        panelWidth = 900		              # pixels
        panelHeight = 255		              # pixels
        zoomlevels = [4, 8, 16, 32]           	      # seconds
        options = coll.namedtuple('options', 'image_height fft_size image_width')
        options.image_height = panelHeight
        options.image_width = panelWidth
        options.fft_size = 31

        ret = {}
        for zoom in zoomlevels:
            wvFile = wave.Wave_read(wavfname)
            framerate = wvFile.getframerate()
            totalframes = wvFile.getnframes()

            # We want this many frames per file at this zoom level.
            framesperimage = framerate * zoom

            wfname = "waveform%s" % zoom
            specname = "spectrum%s" % zoom
            wfdata = []
            specdata = []

            sumframes = 0
            while sumframes < totalframes:
                fp, smallname = tempfile.mkstemp(".wav")
                os.close(fp)
                data = wvFile.readframes(framesperimage)
                wavout = wave.open(smallname, "wb")
                # This will set nframes, but writeframes resets it
                wavout.setparams(wvFile.getparams())
                wavout.writeframes(data)
                wavout.close()
                sumframes += framesperimage

                specio = StringIO()
                # Set the name attr so that PIL gets the filetype hint
                specio.name = "spec.png"
                wavio = StringIO()
                wavio.name = "wav.png"

                w2png.genimages(smallname, wavio, specio, options)
                os.unlink(smallname)

                specdata.append(specio.getvalue())
                wfdata.append(wavio.getvalue())

            ret[wfname] = wfdata
            ret[specname] = specdata

        ret["smallfull"] = self.make_mini(wavfname)
        if created:
            os.unlink(wavfname)

        return ret
コード例 #2
0
ファイル: tasks.py プロジェクト: websiteinspiration/dunya
def make_segments(file_id):
    thefile = models.File.objects.get(pk=file_id)
    patterns = thefile.pattern_set.all()
    wav_file, wav_created = util.docserver_get_wav_filename(thefile.mbid)
    print("Making segments for file %s" % thefile)
    numpat = len(patterns)
    print("Got %s segments to do" % numpat)
    wav_in = wave.open(wav_file, 'rb')
    params = wav_in.getparams()
    samplerate = wav_in.getframerate()

    with transaction.atomic():
        for i, p in enumerate(patterns, 1):
            if i % 100 == 0:
                print("  - [%s]: Done %s/%s" % (file_id, i, numpat))
            r_start = round(p.start_time, 1)
            r_end = round(p.end_time, 1)
            qs = models.Segment.objects.filter(file=thefile,
                                               rounded_start=r_start,
                                               rounded_end=r_end)
            if qs.exists():
                # we already have a segment for this start time and end time
                s = qs.get()
                p.segment = s
                p.save()
            else:
                start_str = str(r_start)
                end_str = str(r_end)
                fname = "%s-%s.wav" % (start_str, end_str)
                dname = start_str[0]

                full_dir = os.path.join(ROOT_DIR, str(thefile.id), dname)
                full_path = os.path.join(full_dir, fname)
                try:
                    os.makedirs(full_dir)
                except os.error:
                    pass

                sframe = int(math.floor(r_start * samplerate))
                eframe = int(math.ceil(r_end * samplerate))
                wav_in.setpos(sframe)
                frames = wav_in.readframes(eframe - sframe)

                # create the file, save it, add a entry
                # args = ["ffmpeg", "-i", wav_file, "-ss", str(r_start), "-t", str((r_end-r_start)), full_path]
                # proc = subprocess.Popen(args, stdin=subprocess.PIPE, stdout=subprocess.PIPE)
                # proc.communicate()
                wav_out = wave.open(full_path, 'wb')
                wav_out.setparams(params)
                wav_out.writeframes(frames)
                s = models.Segment.objects.create(file=thefile,
                                                  rounded_start=r_start,
                                                  rounded_end=r_end,
                                                  segment_path=full_path)
                p.segment = s
                p.save()

    if wav_created:
        os.unlink(wav_file)
コード例 #3
0
ファイル: smallaudioimage.py プロジェクト: MTG/pycompmusic
    def run(self, musicbrainzid, fname):
        wavfname, created = util.docserver_get_wav_filename(musicbrainzid)
        ret = {}
        ret["smallfull"] = self.make_mini(wavfname)
        if created:
            os.unlink(wavfname)

        return ret
コード例 #4
0
    def run(self, musicbrainzid, fname):
        wavfname, created = util.docserver_get_wav_filename(musicbrainzid)
        ret = {}
        ret["smallfull"] = self.make_mini(wavfname)
        if created:
            os.unlink(wavfname)

        return ret
コード例 #5
0
ファイル: tasks.py プロジェクト: joecheriross/dunya
def make_segments(file_id):
    thefile = models.File.objects.get(pk=file_id)
    patterns = thefile.pattern_set.all()
    wav_file, wav_created = util.docserver_get_wav_filename(thefile.mbid)
    print "Making segments for file %s" % thefile
    numpat = len(patterns)
    print "Got %s segments to do" % numpat
    wav_in = wave.open(wav_file, 'rb')
    params = wav_in.getparams()
    samplerate = wav_in.getframerate()

    with transaction.atomic():
        for i, p in enumerate(patterns, 1):
            if i % 100 == 0:
                print "  - [%s]: Done %s/%s" % (file_id, i, numpat)
            r_start = round(p.start_time, 1)
            r_end = round(p.end_time, 1)
            qs = models.Segment.objects.filter(file=thefile, rounded_start=r_start, rounded_end=r_end)
            if qs.exists():
                # we already have a segment for this start time and end time
                s = qs.get()
                p.segment = s
                p.save()
            else:
                start_str = str(r_start)
                end_str = str(r_end)
                fname = "%s-%s.wav" % (start_str, end_str)
                dname = start_str[0]
                
                full_dir = os.path.join(ROOT_DIR, str(thefile.id), dname)
                full_path = os.path.join(full_dir, fname)
                try:
                    os.makedirs(full_dir)
                except os.error:
                    pass

                sframe = int(math.floor(r_start * samplerate))
                eframe = int(math.ceil(r_end * samplerate))
                wav_in.setpos(sframe)
                frames = wav_in.readframes(eframe-sframe)

                # create the file, save it, add a entry
                # args = ["ffmpeg", "-i", wav_file, "-ss", str(r_start), "-t", str((r_end-r_start)), full_path]
                # proc = subprocess.Popen(args, stdin=subprocess.PIPE, stdout=subprocess.PIPE)
                # proc.communicate()
                wav_out = wave.open(full_path, 'wb')
                wav_out.setparams(params)
                wav_out.writeframes(frames)
                s = models.Segment.objects.create(file=thefile, rounded_start=r_start, rounded_end=r_end, segment_path=full_path)
                p.segment = s
                p.save()

    if wav_created:
        os.unlink(wav_file)
コード例 #6
0
ファイル: tasks.py プロジェクト: websiteinspiration/dunya
def fix_segments(file_id):
    thefile = models.File.objects.get(pk=file_id)
    patterns = thefile.pattern_set.all()
    wav_file, wav_created = util.docserver_get_wav_filename(thefile.mbid)
    print("Making segments for file %s" % thefile)
    numpat = len(patterns)
    print("Got %s segments to do" % numpat)
    wav_in = wave.open(wav_file, 'rb')
    params = wav_in.getparams()
    samplerate = wav_in.getframerate()

    with transaction.atomic():
        for i, p in enumerate(patterns, 1):
            if i % 100 == 0:
                print("  - [%s]: Done %s/%s" % (file_id, i, numpat))
            r_start = round(p.start_time, 1)
            r_end = round(p.end_time, 1)
            qs = models.Segment.objects.filter(file=thefile,
                                               rounded_start=r_start,
                                               rounded_end=r_end)
            if qs.exists():
                start_str = str(r_start)
                end_str = str(r_end)
                fname = "%s-%s.wav" % (start_str, end_str)
                dname = start_str[0]

                full_dir = os.path.join(ROOT_DIR, str(thefile.id), dname)
                full_path = os.path.join(full_dir, fname)
                mp3_path = full_path[:-3] + "mp3"

                if os.path.exists(mp3_path) and os.stat(mp3_path).st_size == 0:
                    print("deleting %s" % mp3_path)
                    os.unlink(mp3_path)
                    sframe = int(math.floor(r_start * samplerate))
                    eframe = int(math.ceil(r_end * samplerate))
                    wav_in.setpos(sframe)
                    frames = wav_in.readframes(eframe - sframe)

                    wav_out = wave.open(full_path, 'wb')
                    wav_out.setparams(params)
                    wav_out.writeframes(frames)

    if wav_created:
        os.unlink(wav_file)
コード例 #7
0
ファイル: tasks.py プロジェクト: joecheriross/dunya
def fix_segments(file_id):
    thefile = models.File.objects.get(pk=file_id)
    patterns = thefile.pattern_set.all()
    wav_file, wav_created = util.docserver_get_wav_filename(thefile.mbid)
    print "Making segments for file %s" % thefile
    numpat = len(patterns)
    print "Got %s segments to do" % numpat
    wav_in = wave.open(wav_file, 'rb')
    params = wav_in.getparams()
    samplerate = wav_in.getframerate()

    with transaction.atomic():
        for i, p in enumerate(patterns, 1):
            if i % 100 == 0:
                print "  - [%s]: Done %s/%s" % (file_id, i, numpat)
            r_start = round(p.start_time, 1)
            r_end = round(p.end_time, 1)
            qs = models.Segment.objects.filter(file=thefile, rounded_start=r_start, rounded_end=r_end)
            if qs.exists():
                s = qs.get()

                start_str = str(r_start)
                end_str = str(r_end)
                fname = "%s-%s.wav" % (start_str, end_str)
                dname = start_str[0]
                
                full_dir = os.path.join(ROOT_DIR, str(thefile.id), dname)
                full_path = os.path.join(full_dir, fname)
                mp3_path = full_path[:-3]+"mp3"

                if os.path.exists(mp3_path) and os.stat(mp3_path).st_size == 0:
                    print "deleting", mp3_path
                    os.unlink(mp3_path)
                    sframe = int(math.floor(r_start * samplerate))
                    eframe = int(math.ceil(r_end * samplerate))
                    wav_in.setpos(sframe)
                    frames = wav_in.readframes(eframe-sframe)

                    wav_out = wave.open(full_path, 'wb')
                    wav_out.setparams(params)
                    wav_out.writeframes(frames)

    if wav_created:
        os.unlink(wav_file)
コード例 #8
0
ファイル: audioimages.py プロジェクト: EQ4/pycompmusic
    def run(self, musicbrainzid, fname):
        baseFname, ext = os.path.splitext(os.path.basename(fname))

        wavfname, created = util.docserver_get_wav_filename(musicbrainzid)

        panelWidth = 900  # pixels
        panelHeight = 255  # pixels
        zoomlevels = self._zoom_levels  # seconds
        options = coll.namedtuple("options", "image_height fft_size image_width f_min f_max scale_exp pallete")
        options.image_height = panelHeight
        options.fft_size = self._fft_size
        options.f_min = self._f_min
        options.f_max = self._f_max
        options.pallete = self._pallete
        options.scale_exp = self._scale_exp

        ret = {}
        for zoom in zoomlevels:
            # At the beginning of each zoom level we reset the image_width
            # since we are modifying it at the end of the last zoom level
            options.image_width = panelWidth

            wvFile = wave.Wave_read(wavfname)
            framerate = wvFile.getframerate()
            totalframes = wvFile.getnframes()

            # We want this many frames per file at this zoom level.
            framesperimage = framerate * zoom

            wfname = "waveform%s" % zoom
            specname = "spectrum%s" % zoom
            wfdata = []
            specdata = []

            sumframes = 0
            while sumframes < totalframes:
                if sumframes + framesperimage > totalframes:
                    remaining_frames = totalframes - sumframes
                    options.image_width = options.image_width * remaining_frames / framesperimage
                else:
                    remaining_frames = framesperimage

                fp, smallname = tempfile.mkstemp(".wav")
                os.close(fp)
                data = wvFile.readframes(remaining_frames)
                wavout = wave.open(smallname, "wb")
                # This will set nframes, but writeframes resets it
                wavout.setparams(wvFile.getparams())
                wavout.writeframes(data)
                wavout.close()
                sumframes += framesperimage

                specio = StringIO()
                # Set the name attr so that PIL gets the filetype hint
                specio.name = "spec.png"
                wavio = StringIO()
                wavio.name = "wav.png"

                w2png.genimages(smallname, wavio, specio, options)
                os.unlink(smallname)

                specdata.append(specio.getvalue())
                wfdata.append(wavio.getvalue())

            ret[wfname] = wfdata
            ret[specname] = specdata

        ret["smallfull"] = self.make_mini(wavfname)
        if created:
            os.unlink(wavfname)

        return ret
コード例 #9
0
    def run(self, musicbrainzid, fname):
        baseFname, ext = os.path.splitext(os.path.basename(fname))

        wavfname, created = util.docserver_get_wav_filename(musicbrainzid)

        panelWidth = 900  # pixels
        panelHeight = 255  # pixels
        zoomlevels = self._zoom_levels  # seconds
        options = coll.namedtuple(
            'options',
            'image_height fft_size image_width f_min f_max scale_exp pallete')
        options.image_height = panelHeight
        options.fft_size = self._fft_size
        options.f_min = self._f_min
        options.f_max = self._f_max
        options.pallete = self._pallete
        options.scale_exp = self._scale_exp

        ret = {}
        for zoom in zoomlevels:
            # At the beginning of each zoom level we reset the image_width
            # since we are modifying it at the end of the last zoom level
            options.image_width = panelWidth

            wvFile = wave.Wave_read(wavfname)
            framerate = wvFile.getframerate()
            totalframes = wvFile.getnframes()

            # We want this many frames per file at this zoom level.
            framesperimage = framerate * zoom

            wfname = "waveform%s" % zoom
            specname = "spectrum%s" % zoom
            inv_mfcc_name = "inv_mfcc_spectrum%s" % zoom
            wfdata = []
            specdata = []
            inv_mfcc_data = []

            sumframes = 0
            while sumframes < totalframes:
                if sumframes + framesperimage > totalframes:
                    remaining_frames = (totalframes - sumframes)
                    options.image_width = options.image_width * remaining_frames / framesperimage
                else:
                    remaining_frames = framesperimage

                fp, smallname = tempfile.mkstemp(".wav")
                os.close(fp)
                data = wvFile.readframes(remaining_frames)
                wavout = wave.open(smallname, "wb")
                # This will set nframes, but writeframes resets it
                wavout.setparams(wvFile.getparams())
                wavout.writeframes(data)
                wavout.close()
                sumframes += framesperimage

                specio = io.BytesIO()
                # Set the name attr so that PIL gets the filetype hint
                specio.name = "spec.png"
                wavio = io.BytesIO()
                wavio.name = "wav.png"
                in_mfcc_io = io.BytesIO()
                in_mfcc_io.name = "melspec.png"

                w2png.genimages(smallname, wavio, specio, in_mfcc_io, options)
                os.unlink(smallname)

                specdata.append(specio.getvalue())
                wfdata.append(wavio.getvalue())
                inv_mfcc_data.append(in_mfcc_io.getvalue())

            ret[wfname] = wfdata
            ret[specname] = specdata
            ret[inv_mfcc_name] = inv_mfcc_data

        ret["smallfull"] = self.make_mini(wavfname)
        if created:
            os.unlink(wavfname)

        return ret
コード例 #10
0
    def run(self, musicbrainzid, fname):

        #### output
        ret = {'alignedLyricsSyllables': {}, 'sectionlinks': {}}

        recIDoutputDir = os.path.join(self.dataOutputDir, musicbrainzid)
        if not os.path.isdir(recIDoutputDir):
            os.mkdir(recIDoutputDir)

        w = getWork(musicbrainzid)

        # TODO: mark IDs with second verse symbTr missing and get from a separate reposotory
        # on dunya server. API might be outdated, as some symbtr names are changed. better use line below
        #         symbtrtxtURI = util.docserver_get_symbtrtxt(w['mbid'])

        #  prefer to fetch symbTr directly from github, as Dunya might not be updated with lyrics changes, etc.
        symbtrtxtURI = downloadSymbTr(w['mbid'], recIDoutputDir,
                                      self.hasSecondVerse)

        if not symbtrtxtURI:
            sys.exit("no symbTr found for work {}".format(w['mbid']))

        ############ score section metadata
        if WITH_SECTION_ANNOTATIONS:  #  becasue complying with  score metadata for symbTr1, on which annotations are done
            dir_ = 'scores/metadata/'
            sectionMetadataDict = get_section_metadata_dict(
                w['mbid'], dir_, recIDoutputDir,
                self.hasSectionNumberDiscrepancy)
        else:
            sectionMetadataDict = dunya.docserver.get_document_as_json(
                w['mbid'], "metadata", "metadata", 1,
                version="0.1")  # NOTE: this is default for note onsets

        ##################### audio section annotation  or result from section linking
        if WITH_SECTION_ANNOTATIONS:  #  because complying with section annotations
            try:
                dir_ = 'audio_metadata/'
                sectionLinksDict = get_section_annotaions_dict(
                    musicbrainzid, dir_, self.dataOutputDir,
                    self.hasSectionNumberDiscrepancy)
            except Exception as e:
                sys.exit("no section annotations found for audio {} ".format(
                    musicbrainzid))

        else:
            try:
                sectionLinksDict = dunya.docserver.get_document_as_json(
                    musicbrainzid,
                    "joinanalysis",
                    "sections",
                    1,
                    version="0.2")
            except dunya.conn.HTTPError:
                logging.error("section link {} missing".format(musicbrainzid))
                return ret
            if not sectionLinksDict:
                logging.error("section link {} missing".format(musicbrainzid))
                return ret

        try:
            extractedPitch = dunya.docserver.get_document_as_json(
                musicbrainzid, "jointanalysis", "pitch", 1, version="0.1")
            extractedPitch = extractedPitch['pitch']
        except Exception as e:
            sys.exit("no initialmakampitch series could be downloaded.  ")

        if ON_SERVER:
            from docserver import util
            wavFileURI, created = util.docserver_get_wav_filename(
                musicbrainzid)


#
        else:
            wavFileURI = fetch_audio_wav(self.dataOutputDir, musicbrainzid,
                                         ParametersAlgo.POLYPHONIC)

        wavFileURIMono = stereoToMono(wavFileURI)
        if ParametersAlgo.WITH_ORACLE_ONSETS == 1:
            fetchNoteOnsetFile(musicbrainzid, recIDoutputDir,
                               ParametersAlgo.ANNOTATION_RULES_ONSETS_EXT)

        recording = loadMakamRecording(musicbrainzid, wavFileURIMono,
                                       symbtrtxtURI, sectionMetadataDict,
                                       sectionLinksDict,
                                       WITH_SECTION_ANNOTATIONS)
        lyricsAligner = LyricsAligner(recording, WITH_SECTION_ANNOTATIONS,
                                      PATH_TO_HCOPY)

        totalDetectedTokenList, sectionLinksDict = lyricsAligner.alignRecording(
            extractedPitch, self.dataOutputDir)
        #         lyricsAligner.evalAccuracy()

        ret['alignedLyricsSyllables'] = totalDetectedTokenList
        ret['sectionlinks'] = sectionLinksDict
        print(ret)
        return ret
コード例 #11
0
ファイル: lyricsalign.py プロジェクト: MTG/pycompmusic
    def run(self, musicbrainzid, fname):

        #### output
        ret = {'alignedLyricsSyllables': {}, 'sectionlinks': {}}

        recIDoutputDir = os.path.join(self.dataOutputDir, musicbrainzid)
        if not os.path.isdir(recIDoutputDir):
            os.mkdir(recIDoutputDir)

        w = getWork(musicbrainzid)

        # TODO: mark IDs with second verse symbTr missing and get from a separate reposotory 
        # on dunya server. API might be outdated, as some symbtr names are changed. better use line below
        #         symbtrtxtURI = util.docserver_get_symbtrtxt(w['mbid'])

        #  prefer to fetch symbTr directly from github, as Dunya might not be updated with lyrics changes, etc.
        symbtrtxtURI = downloadSymbTr(w['mbid'], recIDoutputDir, self.hasSecondVerse)

        if not symbtrtxtURI:
            sys.exit("no symbTr found for work {}".format(w['mbid']))

        ############ score section metadata
        if WITH_SECTION_ANNOTATIONS:  # becasue complying with  score metadata for symbTr1, on which annotations are done
            dir_ = 'scores/metadata/'
            sectionMetadataDict = get_section_metadata_dict(w['mbid'], dir_, recIDoutputDir,
                                                            self.hasSectionNumberDiscrepancy)
        else:
            sectionMetadataDict = dunya.docserver.get_document_as_json(w['mbid'], "metadata", "metadata", 1,
                                                                       version="0.1")  # NOTE: this is default for note onsets

        ##################### audio section annotation  or result from section linking
        if WITH_SECTION_ANNOTATIONS:  # because complying with section annotations
            try:
                dir_ = 'audio_metadata/'
                sectionLinksDict = get_section_annotaions_dict(musicbrainzid, dir_, self.dataOutputDir,
                                                               self.hasSectionNumberDiscrepancy)
            except Exception as e:
                sys.exit("no section annotations found for audio {} ".format(musicbrainzid))

        else:
            try:
                sectionLinksDict = dunya.docserver.get_document_as_json(musicbrainzid, "joinanalysis", "sections", 1,
                                                                        version="0.2")
            except dunya.conn.HTTPError:
                logging.error("section link {} missing".format(musicbrainzid))
                return ret
            if not sectionLinksDict:
                logging.error("section link {} missing".format(musicbrainzid))
                return ret

        try:
            extractedPitch = dunya.docserver.get_document_as_json(musicbrainzid, "jointanalysis", "pitch", 1,
                                                                  version="0.1")
            extractedPitch = extractedPitch['pitch']
        except Exception as e:
            sys.exit("no initialmakampitch series could be downloaded.  ")

        if ON_SERVER:
            from docserver import util
            wavFileURI, created = util.docserver_get_wav_filename(musicbrainzid)
        #
        else:
            wavFileURI = fetch_audio_wav(self.dataOutputDir, musicbrainzid, ParametersAlgo.POLYPHONIC)

        wavFileURIMono = stereoToMono(wavFileURI)
        if ParametersAlgo.WITH_ORACLE_ONSETS == 1:
            fetchNoteOnsetFile(musicbrainzid, recIDoutputDir, ParametersAlgo.ANNOTATION_RULES_ONSETS_EXT)

        recording = loadMakamRecording(musicbrainzid, wavFileURIMono, symbtrtxtURI, sectionMetadataDict,
                                       sectionLinksDict, WITH_SECTION_ANNOTATIONS)
        lyricsAligner = LyricsAligner(recording, WITH_SECTION_ANNOTATIONS, PATH_TO_HCOPY)

        totalDetectedTokenList, sectionLinksDict = lyricsAligner.alignRecording(extractedPitch, self.dataOutputDir)
        #         lyricsAligner.evalAccuracy()

        ret['alignedLyricsSyllables'] = totalDetectedTokenList
        ret['sectionlinks'] = sectionLinksDict
        print(ret)
        return ret
コード例 #12
0
                logging.error("section link {} missing".format(musicbrainzid))
                return ret
            if not sectionLinksDict:
                logging.error("section link {} missing".format(musicbrainzid))
                return ret

        try:
            extractedPitch = dunya.docserver.get_document_as_json(
                musicbrainzid, "jointanalysis", "pitch", 1, version="0.1")
            extractedPitch = extractedPitch['pitch']
        except Exception, e:
            sys.exit("no initialmakampitch series could be downloaded.  ")

        if ON_SERVER:
            from docserver import util
            wavFileURI, created = util.docserver_get_wav_filename(
                musicbrainzid)


#
        else:
            wavFileURI = fetch_audio_wav(self.dataOutputDir, musicbrainzid,
                                         ParametersAlgo.POLYPHONIC)

        wavFileURIMono = stereoToMono(wavFileURI)
        if ParametersAlgo.WITH_ORACLE_ONSETS == 1:
            fetchNoteOnsetFile(musicbrainzid, recIDoutputDir,
                               ParametersAlgo.ANNOTATION_RULES_ONSETS_EXT)

        recording = loadMakamRecording(musicbrainzid, wavFileURIMono,
                                       symbtrtxtURI, sectionMetadataDict,
                                       sectionLinksDict,