Esempio n. 1
0
    def SaveChannel(self, parent_filename, period=False):
        """ Save the channel in an audio file.

        :param parent_filename: (str)
        :param period: (bool) Save a portion of the channel only

        """
        s = None
        e = None
        if period is True:
            dlg = PeriodChooser(self, self._prefs, 0., float(self._channel.get_nframes())/float(self._channel.get_framerate()))
            answer = dlg.ShowModal()
            if answer == wx.ID_OK:
                (s, e) = dlg.GetValues()
                try:
                    s = float(s)
                    e = float(e)
                    if e < s:
                        raise Exception
                except Exception:
                    ShowInformation(self, self._prefs, "Error in the definition of the portion of time.", style=wx.ICON_ERROR)
                    return
            dlg.Destroy()
            if answer != wx.ID_OK:
                return

        new_filename = SaveAsAudioFile()

        # If it is the OK response, process the data.
        if new_filename is not None:
            if new_filename == parent_filename:
                ShowInformation(self, self._prefs, "Assigning the current file name is forbidden. Choose a new file name.", style=wx.ICON_ERROR)
                return

            # Create a formatted channel
            try:
                channel = self.ApplyChanges(s, e)
            except Exception as e:
                ShowInformation(self, self._prefs, "Error while formatting the channel: %s" % str(e), style=wx.ICON_ERROR)
                return

            message = "File {:s} saved successfully.".format(new_filename)
            if channel is None:
                channel = self._channel
            else:
                message += "\nYou can now open it with AudioRoamer to see your changes!"

            # Save the channel
            try:
                audio = sppasAudioPCM()
                audio.append_channel(channel)
                sppas.src.audiodata.aio.save(new_filename, audio)
            except Exception as e:
                message = "File not saved. Error: {:s}".format(str(e))
            else:
                # Update members
                self._filename = new_filename

            ShowInformation(self, self._prefs, message, style=wx.ICON_INFORMATION)
Esempio n. 2
0
    def write_audio_tracks(self, ipustrs, ipusaudio, output, extension):
        """ Write the audio in track files.

        :param ipustrs: (IPUsTrs)
        :param ipusaudio: (IPUsAudio)
        :param output: (str) Directory name
        :param extension: (str) Extension of the file name for audio tracks

        """
        if not os.path.exists(output):
            os.mkdir(output)

        if extension is None:
            raise IOError('An extension is required to write audio tracks.')

        if ipusaudio.get_channel() is None:
            return

        try:
            split_tracks = ipusaudio.get_track_data(self.tracks)
        except Exception as e:
            raise Exception('Split into tracks failed: %s' % e)

        names = ipustrs.get_names()

        # Write audio tracks
        for i, split_track in enumerate(split_tracks):
            track_basename = ""
            if len(names) > 0 and len(names[i]) > 0:
                # Specific names are given
                track_basename = os.path.join(output, names[i])
            else:
                track_basename = os.path.join(output, "track_%.06d" % (i + 1))

            track_wavname = track_basename + "." + extension
            audio_out = sppasAudioPCM()
            audio_out.append_channel(ipusaudio.get_channel())
            try:
                sppas.src.audiodata.aio.save_fragment(track_wavname, audio_out,
                                                      split_track)
            except Exception as e:
                raise Exception("Can't write track: %s. Error is %s" %
                                (track_wavname, e))
Esempio n. 3
0
    def write_audio_tracks(self, ipustrs, ipusaudio, output, extension):
        """ Write the audio in track files.

        :param ipustrs: (IPUsTrs)
        :param ipusaudio: (IPUsAudio)
        :param output: (str) Directory name
        :param extension: (str) Extension of the file name for audio tracks

        """
        if not os.path.exists(output):
            os.mkdir(output)

        if extension is None:
            raise IOError('An extension is required to write audio tracks.')

        if ipusaudio.get_channel() is None:
            return

        try:
            split_tracks = ipusaudio.get_track_data(self.tracks)
        except Exception as e:
            raise Exception('Split into tracks failed: %s' % e)

        names = ipustrs.get_names()

        # Write audio tracks
        for i, split_track in enumerate(split_tracks):
            track_basename = ""
            if len(names) > 0 and len(names[i])>0:
                # Specific names are given
                track_basename = os.path.join(output, names[i])
            else:
                track_basename = os.path.join(output, "track_%.06d" % (i+1))

            track_wavname = track_basename+"."+extension
            audio_out = sppasAudioPCM()
            audio_out.append_channel(ipusaudio.get_channel())
            try:
                sppas.src.audiodata.aio.save_fragment(track_wavname, audio_out, split_track)
            except Exception as e:
                raise Exception("Can't write track: %s. Error is %s" % (track_wavname, e))
Esempio n. 4
0
    def SaveChannel(self, parent_filename, period=False):
        """Save the channel in an audio file.

        :param parent_filename: (str)
        :param period: (bool) Save a portion of the channel only

        """
        s = None
        e = None
        if period is True:
            dlg = PeriodChooser(
                self, self._prefs, 0.,
                float(self._channel.get_nframes()) /
                float(self._channel.get_framerate()))
            answer = dlg.ShowModal()
            if answer == wx.ID_OK:
                (s, e) = dlg.GetValues()
                try:
                    s = float(s)
                    e = float(e)
                    if e < s:
                        raise Exception
                except Exception:
                    ShowInformation(
                        self,
                        self._prefs,
                        "Error in the definition of the portion of time.",
                        style=wx.ICON_ERROR)
                    return
            dlg.Destroy()
            if answer != wx.ID_OK:
                return

        new_filename = SaveAsAudioFile()

        # If it is the OK response, process the data.
        if new_filename is not None:
            if new_filename == parent_filename:
                ShowInformation(
                    self,
                    self._prefs,
                    "Assigning the current file name is forbidden. Choose a new file name.",
                    style=wx.ICON_ERROR)
                return

            # Create a formatted channel
            try:
                channel = self.ApplyChanges(s, e)
            except Exception as e:
                ShowInformation(self,
                                self._prefs,
                                "Error while formatting the channel: %s" %
                                str(e),
                                style=wx.ICON_ERROR)
                return

            message = "File {:s} saved successfully.".format(new_filename)
            if channel is None:
                channel = self._channel
            else:
                message += "\nYou can now open it with AudioRoamer to see your changes!"

            # Save the channel
            try:
                audio = sppasAudioPCM()
                audio.append_channel(channel)
                sppas.src.audiodata.aio.save(new_filename, audio)
            except Exception as e:
                message = "File not saved. Error: {:s}".format(str(e))
            else:
                # Update members
                self._filename = new_filename

            ShowInformation(self,
                            self._prefs,
                            message,
                            style=wx.ICON_INFORMATION)
Esempio n. 5
0
freq = 2000 # Hz
r = 0.98
a1 = -2.0 * r * math.cos(freq / (SAMPLE_RATE / 2.0) * math.pi)
a2 = r * r
filter = [a1, a2]
print(filter)

n = audioin.get_nframes()
original = struct.unpack('%dh' % n, audioin.read_frames(n))
original = [s / 2.0**15 for s in original]

result = [0 for i in range(0, len(filter))]
biggest = 1
for sample in original:
        for cpos in range(0, len(filter)):
            sample -= result[len(result) - 1 - cpos] * filter[cpos]
        result.append(sample)
        biggest = max(abs(sample), biggest)

result = [sample / biggest for sample in result]
result = [int(sample * (2.0**15 - 1)) for sample in result]

# ----------------------------------------------------------------------------

audioout = sppasAudioPCM()
channel = sppasChannel(framerate=SAMPLE_RATE,
                       sampwidth=audioin.get_sampwidth(),
                       frames=struct.pack('%dh' % len(result), *result))
audioout.append_channel(channel)
sppas.src.audiodata.aio.save(args.o, audioout)
Esempio n. 6
0
                    help='The position (in number of frames) when begins the mix, don\'t use with -bs')

parser.add_argument("-ef",
                    default=0,
                    metavar="value",
                    type=float,
                    help='The position (in number of frames) when ends the mix, don\'t use with -es')

if len(sys.argv) <= 1:
    sys.argv.append('-h')

args = parser.parse_args()

# ----------------------------------------------------------------------------

audio_out = sppasAudioPCM()
audio = sppas.src.audiodata.aio.open(args.w)

if args.bf and args.bs:
    print("bf option and bs option can't be used at the same time!")
    sys.exit(1)

if args.ef and args.es:
    print("ef option and es option can't be used at the same time!")
    sys.exit(1)

if args.bf:
    begin = args.bf
elif args.bs:
    begin = args.bs*audio.get_framerate()
else:
Esempio n. 7
0
if verbose > 0:
    p.update(1, "")
    del p
if verbose > 1:
    print("\nEnd mixing channels at %s" %
          time.strftime('%d/%m/%y %H:%M:%S', time.localtime()))

# ========================SAVE THE RESULT============================#
p = None
if verbose > 0:
    p = ProcessProgressTerminal()
    p.set_new()
    p.set_header("Saving the output file")
    p.set_fraction(0)
    p.update(0, "")

audio_out = sppasAudioPCM()
audio_out.append_channel(newchannelleft)
audio_out.append_channel(newchannelright)

sppas.src.audiodata.aio.save(args.o, audio_out)

if verbose > 0:
    p.update(1, "")

# ----------------------------------------------------------------------------

if verbose > 1:
    print("\nEnd process at %s",
          time.strftime('%d/%m/%y %H:%M:%S', time.localtime()))
Esempio n. 8
0
freq = 2000  # Hz
r = 0.98
a1 = -2.0 * r * math.cos(freq / (SAMPLE_RATE / 2.0) * math.pi)
a2 = r * r
filter = [a1, a2]
print(filter)

n = audioin.get_nframes()
original = struct.unpack('%dh' % n, audioin.read_frames(n))
original = [s / 2.0**15 for s in original]

result = [0 for i in range(0, len(filter))]
biggest = 1
for sample in original:
    for cpos in range(0, len(filter)):
        sample -= result[len(result) - 1 - cpos] * filter[cpos]
    result.append(sample)
    biggest = max(abs(sample), biggest)

result = [sample / biggest for sample in result]
result = [int(sample * (2.0**15 - 1)) for sample in result]

# ----------------------------------------------------------------------------

audioout = sppasAudioPCM()
channel = sppasChannel(framerate=SAMPLE_RATE,
                       sampwidth=audioin.get_sampwidth(),
                       frames=struct.pack('%dh' % len(result), *result))
audioout.append_channel(channel)
sppas.src.audiodata.aio.save(args.o, audioout)