def get_sound_object(self, sound_id):
     try:
         return Sound.objects.get(id=sound_id)
     except Sound.DoesNotExist:
         raise AudioProcessingException("did not find Sound object with id %s" % sound_id)
     except Exception as e:
         raise AudioProcessingException("unexpected error getting Sound %s from DB" % sound_id)
 def get_sound_path(self):
     sound_path = self.sound.locations('path')
     if settings.USE_PREVIEWS_WHEN_ORIGINAL_FILES_MISSING and not os.path.exists(sound_path):
         sound_path = self.sound.locations('preview.LQ.mp3.path')
     if not os.path.exists(sound_path):
         raise AudioProcessingException('could not find file with path %s' % sound_path)
     self.log_info("file found in " + sound_path)
     return sound_path
Esempio n. 3
0
    def convert_to_pcm(self,
                       sound_path,
                       tmp_directory,
                       force_use_ffmpeg=False,
                       mono=False):
        """
        Convert a given sound file to PCM. By default we first try to use corresponding decoders for each format and
        preserve original file properties (sampling rate, etc). If conversion fails with this method we failback to
        ffmpeg conversion and set standartized 44.1kHz, 16bit output. ffmpeg conversion still preserves number of
        channels but can be optionally set to output a mono file (useful for analysis code)
        :param sound_path: path of the audiofile to convert
        :param force_use_ffmpeg: don't try to use format specific decoder and go straight to ffmpeg
        :param mono: output mono file (only applies when using ffmpeg conversion)
        :return: path of the converted audio file
        """
        # Convert to PCM and save PCM version in `tmp_wavefile`
        try:
            fh, tmp_wavefile = tempfile.mkstemp(suffix=".wav",
                                                prefix="%i_" % self.sound.id,
                                                dir=tmp_directory)
            # Close file handler as we don't use it from Python
            os.close(fh)
            if force_use_ffmpeg:
                raise AudioProcessingException(
                )  # Go to directly to ffmpeg conversion
            if not audioprocessing.convert_to_pcm(sound_path, tmp_wavefile):
                tmp_wavefile = sound_path
                self.log_info(
                    "no need to convert, this file is already PCM data")

        except IOError as e:
            # Could not create tmp file
            raise AudioProcessingException(
                "could not create tmp_wavefile file: %s" % e)
        except OSError as e:
            raise AudioProcessingException(
                "conversion to PCM failed, "
                "make sure that format conversion executables exist: %s" % e)
        except AudioProcessingException as e:
            # Conversion with format codecs has failed (or skipped using 'force_use_ffmpeg' argument)
            self.log_info(
                "conversion to PCM failed or skipped, now trying conversion with ffmpeg"
            )
            try:
                audioprocessing.convert_using_ffmpeg(sound_path,
                                                     tmp_wavefile,
                                                     mono_out=mono)
            except AudioProcessingException as e:
                raise AudioProcessingException("conversion to PCM failed: %s" %
                                               e)
            except OSError as e:
                raise AudioProcessingException(
                    "conversion to PCM failed, "
                    "make sure that ffmpeg executable exists: %s" % e)
        except Exception as e:
            raise AudioProcessingException(
                "unhandled exception while converting to PCM: %s" % e)

        self.log_info("PCM file path: " + tmp_wavefile)
        return tmp_wavefile
Esempio n. 4
0
 def alarm_handler(signum, frame):
     raise AudioProcessingException("timeout while waiting for Essentia")