コード例 #1
0
ファイル: serializers.py プロジェクト: koalamm638/TimeSide
    def to_representation(self, obj):
        request = self.context['request']
        self.subprocessor_id = str(request.GET.get('subprocessor_id', ''))
        self.start = float(request.GET.get('start', 0))
        self.stop = float(request.GET.get('stop', -1))
        self.width = int(float(request.GET.get('width', 1024)))
        self.height = int(float(request.GET.get('height', 128)))

        if not self.subprocessor_id:
            self.subprocessor_id = self.get_subprocessor_id(obj)

        if not obj.has_hdf5():
            raise serializers.ValidationError(
                "result must have an hdf5 file to be visualized")

        hdf5_result = h5py.File(obj.hdf5.path, 'r').get(self.subprocessor_id)
        result = AnalyzerResult().from_hdf5(hdf5_result)
        duration = hdf5_result['audio_metadata'].attrs['duration']
        samplerate = hdf5_result['data_object']['frame_metadata'].attrs[
            'samplerate']

        if self.start < 0:
            self.start = 0
        if self.start > duration:
            raise serializers.ValidationError(
                "start must be less than duration")
        if self.stop == -1:
            self.stop = duration

            if self.stop > duration:
                self.stop = duration

        # following same cap_value for width
        # as for waveform's nb_pixel serialization
        cap_value = int(samplerate * abs(self.stop - self.start) / 2)
        if self.width > cap_value:
            self.width = cap_value

        from io import BytesIO
        pil_image = result._render_PIL(size=(self.width, self.height),
                                       dpi=80,
                                       xlim=(self.start, self.stop))
        image_buffer = BytesIO()
        pil_image.save(image_buffer, 'PNG')
        return image_buffer.getvalue()
コード例 #2
0
    def to_representation(self, obj):
        subprocessor_id = self.context.get('request').query_params.get('id')

        start = float(self.context.get('request').query_params.get('start', 0))
        stop = float(self.context.get('request').query_params.get('stop', -1))
        width = int(self.context.get(
            'request').query_params.get('width', 1024))
        height = int(self.context.get(
            'request').query_params.get('height', 128))

        import h5py
        hdf5_result = h5py.File(obj.hdf5.path, 'r').get(subprocessor_id)
        from timeside.core.analyzer import AnalyzerResult
        result = AnalyzerResult().from_hdf5(hdf5_result)
        duration = hdf5_result['audio_metadata'].attrs['duration']

        if start < 0:
            start = 0
        if start > duration:
            raise serializers.ValidationError(
                "start must be less than duration")
        if stop == -1:
            stop = duration

            if stop > duration:
                stop = duration

        if True:
            # if result.data_object.y_value.size:

            import StringIO
            pil_image = result._render_PIL(
                size=(width, height), dpi=80, xlim=(start, stop))
            image_buffer = StringIO.StringIO()
            pil_image.save(image_buffer, 'PNG')
            return image_buffer.getvalue()
        else:
            return result.to_json()