<commit_before>from av.utils cimport err_check, samples_alloc_array_and_samples, channel_layout_name


cdef class SwrContextProxy(object):
    def __dealloc__(self):
        lib.swr_free(&self.ptr)


cdef class AudioFrame(Frame):

    """A frame of audio."""
    
    def __dealloc__(self):
        # These are all NULL safe.
        if self.buffer_:
            lib.av_freep(&self.buffer_[0])
        lib.av_freep(&self.buffer_)
    
    def __repr__(self):
        return '<%s.%s nb_samples:%d %dhz %s %s at 0x%x>' % (
            self.__class__.__module__,
            self.__class__.__name__,
            self.samples,
            self.sample_rate,
            self.channel_layout,
            self.sample_fmt,
            id(self),
        )
        
    def __init__(self, *args):
        super(AudioFrame, self).__init__(*args)
        self.align = 1
        
    cdef alloc_frame(self, int channels, lib.AVSampleFormat sample_fmt, int nb_samples):
     
        if self.ptr:
            raise MemoryError("frame already allocated")
        cdef int ret
        cdef int linesize
        
        self.ptr= lib.avcodec_alloc_frame()
        lib.avcodec_get_frame_defaults(self.ptr)
        
        ret = samples_alloc_array_and_samples(&self.buffer_, 
                                              &linesize,
                                              channels,
                                              nb_samples,
                                              sample_fmt,
                                              self.align)
        if ret < 0:
            raise MemoryError("error samples_alloc_array_and_samples: %i" % ret)
        #NEED TO SET CHANNEL LAYOUT!
        #self.ptr.channels = channels
        self.ptr.format = <int > sample_fmt
        self.ptr.nb_samples = nb_samples
                
        
    cdef fill_frame(self, int nb_samples):
        if not self.ptr:
            raise MemoryError("Frame Not allocated")
        
        self.ptr.nb_samples = nb_samples

        samples_size = lib.av_samples_get_buffer_size(NULL,
                                                       self.channels,
                                                       self.ptr.nb_samples,
                                                       <lib.AVSampleFormat>self.ptr.format,self.align)
        
        err_check(lib.avcodec_fill_audio_frame(self.ptr, 
                                             self.channels, 
                                             <lib.AVSampleFormat> self.ptr.format,
                                             self.buffer_[0],
                                             samples_size, self.align))
        
        self.buffer_size = samples_size
        
    def set_silence(self, int offset, int nb_samples):
        
        err_check(lib.av_samples_set_silence(self.ptr.extended_data,
                                             offset,
                                             nb_samples,
                                             self.channels,
                                             <lib.AVSampleFormat>self.ptr.format))
    
    cpdef resample(self, bytes channel_layout, bytes sample_fmt, int out_sample_rate):
        
        
        # Check params
        cdef uint64_t out_ch_layout = lib.av_get_channel_layout(channel_layout)
        if out_ch_layout == 0:
            raise ValueError("invalid channel layout %s" % channel_layout)
            
        cdef lib.AVSampleFormat out_sample_fmt = lib.av_get_sample_fmt(sample_fmt)
        if out_sample_fmt == lib.AV_SAMPLE_FMT_NONE:
            raise ValueError("invalid sample_fmt %s" % sample_fmt)
        
        if not self.swr_proxy:
            self.swr_proxy = SwrContextProxy()
        
        cdef int dst_nb_channels = lib.av_get_channel_layout_nb_channels(out_ch_layout)
            
        #print "source =", self.sample_rate, self.channel_layout,self.ptr.channel_layout, self.channels, self.sample_fmt,self.ptr.format
        #print "dest   =", out_sample_rate, channel_layout,out_ch_layout, dst_nb_channels, sample_fmt, out_sample_fmt

        if not self.swr_proxy.ptr:
            self.swr_proxy.ptr = lib.swr_alloc()
        
        err_check(lib.av_opt_set_int(self.swr_proxy.ptr, "in_channel_layout" ,self.ptr.channel_layout,0))
        err_check(lib.av_opt_set_int(self.swr_proxy.ptr, "out_channel_layout" ,out_ch_layout,0))
        
        err_check(lib.av_opt_set_int(self.swr_proxy.ptr, 'in_sample_rate', self.ptr.sample_rate, 0))
        err_check(lib.av_opt_set_int(self.swr_proxy.ptr, 'out_sample_rate', out_sample_rate, 0))
        
        err_check(lib.av_opt_set_int(self.swr_proxy.ptr, 'in_sample_fmt', self.ptr.format, 0))
        err_check(lib.av_opt_set_int(self.swr_proxy.ptr, 'out_sample_fmt', <int>out_sample_fmt, 0))
        
        err_check(lib.swr_init(self.swr_proxy.ptr))
        
        # helper names, just so I remember what they are       
        cdef int src_nb_samples = self.ptr.nb_samples
        cdef int src_rate = self.ptr.sample_rate
        
        # compute the number of converted samples
        cdef int dst_nb_samples = lib.av_rescale_rnd(src_nb_samples,
                                                 out_sample_rate, #dst sample rate
                                                 src_rate, # src sample rate
                                                 lib.AV_ROUND_UP)
        
        cdef AudioFrame frame
        
        # create a audio fifo queue to collect samples
        cdef AudioFifo fifo = AudioFifo(channel_layout,
                                        sample_fmt, 
                                        out_sample_rate,
                                        dst_nb_samples)
        
        flush = False
        
        # NOTE: for some reason avresample_convert won't return enough converted samples if src_nb_samples
        # is the correct size, this hack fixes that, its not safe for use with swr_convert
        if lib.USING_AVRESAMPLE:
            src_nb_samples += 1000
        
        while True:
            frame = AudioFrame()
            
            # allocate the correct frame size
            frame.alloc_frame( dst_nb_channels, out_sample_fmt, dst_nb_samples)
            frame.fill_frame(dst_nb_samples)

            # Note: swr_convert returns number of samples output per channel,
            # negative value on error
            
            if not flush:
                ret = err_check(lib.swr_convert(self.swr_proxy.ptr,
                                      frame.ptr.extended_data,dst_nb_samples,
                                      self.ptr.extended_data, src_nb_samples))
                
            # Flush any remaining samples out
            else:         
                 ret = err_check(lib.swr_convert(self.swr_proxy.ptr,
                                       frame.ptr.extended_data,dst_nb_samples,
                                       NULL, 0))
<commit_msg>Use err_check<commit_after>from av.utils cimport err_check, samples_alloc_array_and_samples, channel_layout_name


cdef class SwrContextProxy(object):
    def __dealloc__(self):
        lib.swr_free(&self.ptr)


cdef class AudioFrame(Frame):

    """A frame of audio."""
    
    def __dealloc__(self):
        # These are all NULL safe.
        if self.buffer_:
            lib.av_freep(&self.buffer_[0])
        lib.av_freep(&self.buffer_)
    
    def __repr__(self):
        return '<%s.%s nb_samples:%d %dhz %s %s at 0x%x>' % (
            self.__class__.__module__,
            self.__class__.__name__,
            self.samples,
            self.sample_rate,
            self.channel_layout,
            self.sample_fmt,
            id(self),
        )
        
    def __init__(self, *args):
        super(AudioFrame, self).__init__(*args)
        self.align = 1
        
    cdef alloc_frame(self, int channels, lib.AVSampleFormat sample_fmt, int nb_samples):
     
        if self.ptr:
            return

        cdef int ret
        cdef int linesize
        
        self.ptr = lib.avcodec_alloc_frame()
        lib.avcodec_get_frame_defaults(self.ptr)
        
        err_check(samples_alloc_array_and_samples(
            &self.buffer_, 
            &linesize,
            channels,
            nb_samples,
            sample_fmt,
            self.align,
        ))

        # TODO: Set channel layout.
        self.ptr.format = <int > sample_fmt
        self.ptr.nb_samples = nb_samples
                
        
    cdef fill_frame(self, int nb_samples):
        if not self.ptr:
            raise MemoryError("Frame Not allocated")
        
        self.ptr.nb_samples = nb_samples

        samples_size = lib.av_samples_get_buffer_size(NULL,
                                                       self.channels,
                                                       self.ptr.nb_samples,
                                                       <lib.AVSampleFormat>self.ptr.format,self.align)
        
        err_check(lib.avcodec_fill_audio_frame(self.ptr, 
                                             self.channels, 
                                             <lib.AVSampleFormat> self.ptr.format,
                                             self.buffer_[0],
                                             samples_size, self.align))
        
        self.buffer_size = samples_size
        
    def set_silence(self, int offset, int nb_samples):
        
        err_check(lib.av_samples_set_silence(self.ptr.extended_data,
                                             offset,
                                             nb_samples,
                                             self.channels,
                                             <lib.AVSampleFormat>self.ptr.format))
    
    cpdef resample(self, bytes channel_layout, bytes sample_fmt, int out_sample_rate):
        
        
        # Check params
        cdef uint64_t out_ch_layout = lib.av_get_channel_layout(channel_layout)
        if out_ch_layout == 0:
            raise ValueError("invalid channel layout %s" % channel_layout)
            
        cdef lib.AVSampleFormat out_sample_fmt = lib.av_get_sample_fmt(sample_fmt)
        if out_sample_fmt == lib.AV_SAMPLE_FMT_NONE:
            raise ValueError("invalid sample_fmt %s" % sample_fmt)
        
        if not self.swr_proxy:
            self.swr_proxy = SwrContextProxy()
        
        cdef int dst_nb_channels = lib.av_get_channel_layout_nb_channels(out_ch_layout)
            
        #print "source =", self.sample_rate, self.channel_layout,self.ptr.channel_layout, self.channels, self.sample_fmt,self.ptr.format
        #print "dest   =", out_sample_rate, channel_layout,out_ch_layout, dst_nb_channels, sample_fmt, out_sample_fmt

        if not self.swr_proxy.ptr:
            self.swr_proxy.ptr = lib.swr_alloc()
        
        err_check(lib.av_opt_set_int(self.swr_proxy.ptr, "in_channel_layout" ,self.ptr.channel_layout,0))
        err_check(lib.av_opt_set_int(self.swr_proxy.ptr, "out_channel_layout" ,out_ch_layout,0))
        
        err_check(lib.av_opt_set_int(self.swr_proxy.ptr, 'in_sample_rate', self.ptr.sample_rate, 0))
        err_check(lib.av_opt_set_int(self.swr_proxy.ptr, 'out_sample_rate', out_sample_rate, 0))
        
        err_check(lib.av_opt_set_int(self.swr_proxy.ptr, 'in_sample_fmt', self.ptr.format, 0))
        err_check(lib.av_opt_set_int(self.swr_proxy.ptr, 'out_sample_fmt', <int>out_sample_fmt, 0))
        
        err_check(lib.swr_init(self.swr_proxy.ptr))
        
        # helper names, just so I remember what they are       
        cdef int src_nb_samples = self.ptr.nb_samples
        cdef int src_rate = self.ptr.sample_rate
        
        # compute the number of converted samples
        cdef int dst_nb_samples = lib.av_rescale_rnd(src_nb_samples,
                                                 out_sample_rate, #dst sample rate
                                                 src_rate, # src sample rate
                                                 lib.AV_ROUND_UP)
        
        cdef AudioFrame frame
        
        # create a audio fifo queue to collect samples
        cdef AudioFifo fifo = AudioFifo(channel_layout,
                                        sample_fmt, 
                                        out_sample_rate,
                                        dst_nb_samples)
        
        flush = False
        
        # NOTE: for some reason avresample_convert won't return enough converted samples if src_nb_samples
        # is the correct size, this hack fixes that, its not safe for use with swr_convert
        if lib.USING_AVRESAMPLE:
            src_nb_samples += 1000
        
        while True:
            frame = AudioFrame()
            
            # allocate the correct frame size
            frame.alloc_frame(dst_nb_channels, out_sample_fmt, dst_nb_samples)
            frame.fill_frame(dst_nb_samples)

            # Note: swr_convert returns number of samples output per channel,
            # negative value on error
            
            if not flush:
                ret = err_check(lib.swr_convert(self.swr_proxy.ptr,
                                      frame.ptr.extended_data,dst_nb_samples,
                                      self.ptr.extended_data, src_nb_samples))
                
            # Flush any remaining samples out
            else:         
                 ret = err_check(lib.swr_convert(self.swr_proxy.ptr,
                                       frame.ptr.extended_data,dst_nb_samples,
                                       NULL, 0))

{'fraction_non_alphanumeric': 0.07027106658809665, 'fraction_numerical': 0.002725397760754272, 'mean_word_length': 1.4353363228699552, 'pattern_counts': {'":': 0, '<': 16, '<?xml version=': 0, '>': 15, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 80, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '7203820', 'n_tokens_mistral': 3720, 'n_tokens_neox': 3642, 'n_words': 706}