예제 #1
0
    def split(self, remove=True):
        """Split a non-single channeled file into mono files.

        Create a new mono audio file for each channel of the original
        one, mainly used for DAWs that distinguish between stereo audio
        files from mono ones.

        Parameters
        ----------
        remove : bool, optional
            Remove the original file from system, default to True.
        """
        if self.file and self.channels > 1:
            channelnums = ("L",
                           "R") if self.channels == 2 else range(self.channels)
            for i, ch in enumerate(channelnums):
                self.file.seek(0)
                data = self.file.read()[i]
                st = self.file.subtype
                ed = self.file.endian
                fm = self.file.format
                with sf(
                        self.root + self.delimiter + ch + self.extension,
                        "w",
                        self._samplerate,
                        1,
                        st,
                        ed,
                        fm,
                        True,
                ) as f:
                    f.write(data)
            if remove:
                self.remove(forced=True)
예제 #2
0
 def file(self, file):
     self._file = sf(file)
     if not self.blocksize:
         self.blocksize = self._file.samplerate
     if self._file.channels <= 2:
         self._channel = self._check_mono()
         LOGGER.info(
             'Finished Analyzing channel properties: channel == %s; empty == %s; fake == %s;',
             self.channel, self.isEmpty, self.isFakeStereo)
         self._file.seek(0)
예제 #3
0
파일: FT.py 프로젝트: amr1235/signalViewer
 def fn_CreateSoundFile(arr_of_realNum, samplrate):
     file_handle = sf("Tdfgjdli.wav",
                      mode='w',
                      samplerate=samplrate,
                      channels=1,
                      subtype=None,
                      endian='FILE',
                      format='WAV',
                      closefd=True)
     file_handle.write(arr_of_realNum)
     file_handle.close()
예제 #4
0
 def monolize(self, channel=None):
     if self.file and (channel or (self.isMono and self.isFakeStereo)):
         channel = channel or self.channel
         LOGGER.info('Monolizing fake stereo file.')
         data = [x[self.channel] for x in self.file.read()]
         self.file.close()
         with sf(self.filename, 'w', self.file.samplerate, 1,
                 self.file.subtype, self.file.endian, self.file.format,
                 True) as f:
             f.write(data)
         LOGGER.info('Mono file %s created.', self.filename)
         self.file = self.filename
예제 #5
0
 def file(self, file):
     try:
         self._file = sf(file)
     except RuntimeError:
         self.close()
     else:
         self._channels = self._file.channels
         self._samplerate = self._file.samplerate
         if not self.blocksize:
             self.blocksize = self._file.frames
         self.analyze()
         self._file.seek(0)
예제 #6
0
    def monoize(self, channel=None):
        """Convert a non-mono audio file to single-channel one.

        Parameters
        ----------
        channel : int, optional
            The channel to keep manually. If no valid input received,
            the channel with the highest amplitude is selected, if the
            file is a fake stereo one.
        """
        if self.file and (channel or self.isFakeStereo):
            channel = channel or self._validChannel - 1
            data = [x[channel] for x in self.file.read()]
            self.file.close()
            st = self.file.subtype
            ed = self.file.endian
            fm = self.file.format
            with sf(self._filepath, "w", self._samplerate, 1, st, ed, fm,
                    True) as f:
                f.write(data)
            self.file = self._filepath
예제 #7
0
 def join_old(self, other=None, remove=True):
     s = re.match(r"(.+)([^\a])([lL]|[rR])$", self.root)
     if s:
         base, delimiter, ch = s.groups()
         chs = ["L", "R"]
         chnum = chs.index(ch)
         data = self.file.read(always_2d=True)
         chs.remove(ch)
         newfile = base + delimiter + chs[0] + self.extension
         if not os.path.exists(newfile):
             return
         with AudioFile(newfile) as f:
             if chnum:
                 data = np.concatenate((f.file.read(always_2d=True), data),
                                       axis=1)
             else:
                 data = np.concatenate((data, f.file.read(always_2d=True)),
                                       axis=1)
             if remove:
                 f.remove()
         st = self.file.subtype
         ed = self.file.endian
         fm = self.file.format
         with sf(
                 base + self.extension,
                 "w",
                 self._samplerate,
                 2,
                 st,
                 ed,
                 fm,
                 True,
         ) as f:
             f.write(data)
         if remove:
             self.close()
             os.remove(self._filepath)
예제 #8
0
    def join(self, others=None, remove=True, forced=False, newfile=None):
        """Join several files together into a new one.

        Used for joining audio file of the same source but with different
        channels (multi-mic recordings) into a single multi-channeled or
        stereo file.

        Parameters
        ----------
        others : [str, AudioFile], optional
            List of other files to join, abort action if no other files
            are received.
        remove : bool, optional
            Whether to delete the original files after joining, by
            default True.
        forced : bool, optional
            Whether to join files even when they have different length,
            by default False.
        newfile : str, optional
            The absolute location of the new file, if no input, use path
            + filebase of self.

        Returns
        -------
        str
            The absolute location of the new file.

        Raises
        ------
        FileNotFoundError
            Any file not found in others will raise this error.
        """
        if not others and not self.join_files:
            return

        if not others:
            others = self.join_files

        if not isinstance(others, list):
            others = [others]

        data = None
        a = [self]
        max_frames = self.frames
        for each in others:
            if not isinstance(each, AudioFile):
                if not os.path.exists(each):
                    raise FileNotFoundError
                each = AudioFile(each)
            a.append(each)
            max_frames = max(max_frames, each.frames)

        newfile = self.get_newfile_path(newfile)

        st = self.file.subtype
        ed = self.file.endian
        fm = self.file.format

        for each in a:
            if each.frames != self.frames and not forced:
                return
            d = each.file.read(always_2d=True)
            if each.frames < max_frames and forced:
                np.pad(d, (0, max_frames - each.frames), "constant")
            data = d if data is None else np.concatenate((data, d), axis=1)

        with sf(newfile, "w", self._samplerate, 1 + len(others), st, ed, fm,
                True) as f:
            f.write(data)
        if remove:
            for each in a:
                each.remove(forced=True)

        try:
            del self.location
        except AttributeError:
            pass
        self.file = newfile
        return self