def write_audio(self, output_path, key=None, spk=None, audio=None):
        """
        This function writes an audio signal to an output file, applying necessary transformations
        such as resampling, channel handling, and format conversion based on the provided parameters
        and the instance's internal settings.
        
        Args:
            output_path (str): The file path where the audio will be saved.
            key (str, optional): The key used to retrieve audio from the internal result dictionary
                                  if audio is not provided.
            spk (str, optional): A specific speaker identifier, used to extract a particular speaker's
                                 audio from a multi-speaker dataset or result.
            audio (numpy.ndarray, optional): A numpy array containing the audio data to be written.
                                 If provided, key and spk are ignored.
        """
        
        if audio is not None:
            if spk is not None:
                result_ = audio[spk]
            else:
                result_ = audio
        else:
            if spk is not None:
                result_ = self.result[key][spk]
            else:
                result_ = self.result[key]
                
        if self.data['sample_rate'] != self.args.sampling_rate:
            if self.data['channels'] == 2:
                left_channel = librosa.resample(result_[0,:], orig_sr=self.args.sampling_rate, target_sr=self.data['sample_rate'])
                right_channel = librosa.resample(result_[1,:], orig_sr=self.args.sampling_rate, target_sr=self.data['sample_rate'])
                result = np.vstack((left_channel, right_channel)).T
            else:
                result = librosa.resample(result_[0,:], orig_sr=self.args.sampling_rate, target_sr=self.data['sample_rate'])
        else:
            if self.data['channels'] == 2:
                left_channel = result_[0,:]
                right_channel = result_[1,:]
                result = np.vstack((left_channel, right_channel)).T
            else:
                result = result_[0,:]
                
        if self.data['sample_width'] == 4: ##32 bit float
            MAX_WAV_VALUE = 2147483648.0
            np_type = np.int32
        elif self.data['sample_width'] == 2: ##16 bit int
            MAX_WAV_VALUE = 32768.0
            np_type = np.int16
        else:
            self.data['sample_width'] = 2 ##16 bit int
            MAX_WAV_VALUE = 32768.0
            np_type = np.int16
                        
        result = result * MAX_WAV_VALUE
        result = result.astype(np_type)
        audio_segment = AudioSegment(
            result.tobytes(),  # Raw audio data as bytes
            frame_rate=self.data['sample_rate'],  # Sample rate
            sample_width=self.data['sample_width'],          # No. bytes per sample
            channels=self.data['channels']               # No. channels
        )
        audio_format = 'ipod' if self.data['ext'] in ['m4a', 'aac'] else self.data['ext']
        audio_segment.export(output_path, format=audio_format)

        This function writes an audio signal to an output file, applying necessary transformations
        such as resampling, channel handling, and format conversion based on the provided parameters
        and the instance's internal settings.
        
        Args:
            output_path (str): The file path where the audio will be saved.
            key (str, optional): The key used to retrieve audio from the internal result dictionary
                                  if audio is not provided.
            spk (str, optional): A specific speaker identifier, used to extract a particular speaker's
                                 audio from a multi-speaker dataset or result.
            audio (numpy.ndarray, optional): A numpy array containing the audio data to be written.
                                 If provided, key and spk are ignored.
        
clearvoice/clearvoice/networks.py