示例#1
0
def synthesize(frames,
               sms_header,
               synth_type=pysms.SMS_STYPE_ALL,
               det_synth_type=pysms.SMS_DET_IFFT,
               hop_size=pysms.SMS_MIN_SIZE_FRAME):
    pysms.sms_init()
    synth_params = pysms.SMS_SynthParams()
    pysms.sms_initSynthParams(synth_params)
    synth_params.iSynthesisType = synth_type
    synth_params.iDetSynthType = det_synth_type
    synth_params.sizeHop = hop_size
    pysms.sms_initSynth(sms_header, synth_params)
    interp_frame = pysms.SMS_Data()
    pysms.sms_allocFrameH(sms_header, interp_frame)

    synth_samples = np.zeros(synth_params.sizeHop, dtype=np.float32)
    num_synth_samples = 0
    target_synth_samples = len(frames) * synth_params.origSizeHop
    audio_output = np.array([], dtype=np.float32)
    loc_incr = 1.0 / synth_params.origSizeHop
    current_frame = 0

    # Synthesis loop
    while num_synth_samples < target_synth_samples:
        interp_factor = loc_incr * num_synth_samples
        left_frame_loc = int(
            min(sms_header.nFrames - 1, np.floor(interp_factor)))
        if (left_frame_loc < sms_header.nFrames - 2):
            right_frame_loc = left_frame_loc + 1
        else:
            right_frame_loc = left_frame_loc
        left_frame = frames[left_frame_loc]
        right_frame = frames[right_frame_loc]
        pysms.sms_interpolateFrames(left_frame, right_frame, interp_frame,
                                    interp_factor - left_frame_loc)
        pysms.sms_synthesize(interp_frame, synth_samples, synth_params)
        audio_output = np.hstack((audio_output, synth_samples))
        num_synth_samples += synth_params.sizeHop

    pysms.sms_freeFrame(interp_frame)
    pysms.sms_freeSynth(synth_params)
    pysms.sms_free()
    return audio_output
示例#2
0
def synthesize(frames, sms_header, synth_type=pysms.SMS_STYPE_ALL,
               det_synth_type=pysms.SMS_DET_IFFT, 
               hop_size=pysms.SMS_MIN_SIZE_FRAME): 
    pysms.sms_init() 
    synth_params = pysms.SMS_SynthParams() 
    pysms.sms_initSynthParams(synth_params)
    synth_params.iSynthesisType = synth_type
    synth_params.iDetSynthType = det_synth_type 
    synth_params.sizeHop = hop_size 
    pysms.sms_initSynth(sms_header, synth_params)
    interp_frame = pysms.SMS_Data() 
    pysms.sms_allocFrameH(sms_header, interp_frame)

    synth_samples = np.zeros(synth_params.sizeHop, dtype=np.float32)
    num_synth_samples = 0
    target_synth_samples = len(frames) * synth_params.origSizeHop
    audio_output = np.array([], dtype=np.float32)
    loc_incr = 1.0 / synth_params.origSizeHop
    current_frame = 0

    # Synthesis loop
    while num_synth_samples < target_synth_samples:
        interp_factor = loc_incr * num_synth_samples
        left_frame_loc = int(min(sms_header.nFrames - 1, np.floor(interp_factor)))
        if(left_frame_loc < sms_header.nFrames - 2): 
            right_frame_loc = left_frame_loc + 1
        else: 
            right_frame_loc = left_frame_loc
        left_frame = frames[left_frame_loc]
        right_frame = frames[right_frame_loc]
        pysms.sms_interpolateFrames(left_frame, right_frame, interp_frame, interp_factor - left_frame_loc)
        pysms.sms_synthesize(interp_frame, synth_samples, synth_params)
        audio_output = np.hstack((audio_output, synth_samples))
        num_synth_samples += synth_params.sizeHop
          
    pysms.sms_freeFrame(interp_frame)
    pysms.sms_freeSynth(synth_params)
    pysms.sms_free()
    return audio_output
示例#3
0
def sms_residual_synthesis():
    pysms.sms_init()
    snd_header = pysms.SMS_SndHeader()

    if(pysms.sms_openSF(audio_path, snd_header)):
        raise NameError(pysms.sms_errorString())

    analysis_params = _pysms_analysis_params(sampling_rate)
    analysis_params.nStochasticCoeff = 128
    analysis_params.iStochasticType = pysms.SMS_STOC_APPROX
    if pysms.sms_initAnalysis(analysis_params, snd_header) != 0:
        raise Exception("Error allocating memory for analysis_params")
    analysis_params.iSizeSound = num_samples
    analysis_params.nFrames = num_frames
    sms_header = pysms.SMS_Header()
    pysms.sms_fillHeader(sms_header, analysis_params, "pysms")

    sample_offset = 0
    size_new_data = 0
    current_frame = 0
    analysis_frames = []
    do_analysis = True

    while do_analysis and (current_frame < num_frames):
        sample_offset += size_new_data
        size_new_data = analysis_params.sizeNextRead

        frame_audio = audio[sample_offset:sample_offset + size_new_data]
        frame_audio = np.array(frame_audio, dtype=np.float32)
        if len(frame_audio) < size_new_data:
            frame_audio = np.hstack((
                frame_audio, np.zeros(size_new_data - len(frame_audio),
                                      dtype=np.float32)
            ))

        analysis_data = pysms.SMS_Data()
        pysms.sms_allocFrameH(sms_header, analysis_data)
        status = pysms.sms_analyze(frame_audio, analysis_data,
                                   analysis_params)

        analysis_frames.append(analysis_data)
        current_frame += 1

        if status == -1:
            do_analysis = False

    sms_header.nFrames = len(analysis_frames)
    synth_params = _pysms_synthesis_params(sampling_rate)
    synth_params.iStochasticType = pysms.SMS_STOC_APPROX
    synth_params.iSynthesisType = pysms.SMS_STYPE_STOC
    pysms.sms_initSynth(sms_header, synth_params)

    synth_frame = np.zeros(synth_params.sizeHop, dtype=np.float32)
    synth_audio = np.array([], dtype=np.float32)

    for i in range(len(analysis_frames)):
        pysms.sms_synthesize(analysis_frames[i], synth_frame, synth_params)
        synth_audio = np.hstack((synth_audio, synth_frame))

    synth_audio = np.asarray(synth_audio * 32768, np.int16)

    for frame in analysis_frames:
        pysms.sms_freeFrame(frame)
    pysms.sms_freeAnalysis(analysis_params)
    pysms.sms_closeSF()
    pysms.sms_freeSynth(synth_params)
    pysms.sms_free()

    return synth_audio
示例#4
0
def sms_harmonic_synthesis(det_synth_type):
    pysms.sms_init()
    snd_header = pysms.SMS_SndHeader()

    if(pysms.sms_openSF(audio_path, snd_header)):
        raise NameError(pysms.sms_errorString())

    analysis_params = _pysms_analysis_params(sampling_rate)
    if pysms.sms_initAnalysis(analysis_params, snd_header) != 0:
        raise Exception("Error allocating memory for analysis_params")
    analysis_params.iSizeSound = num_samples
    analysis_params.nFrames = num_frames
    sms_header = pysms.SMS_Header()
    pysms.sms_fillHeader(sms_header, analysis_params, "pysms")

    sample_offset = 0
    size_new_data = 0
    current_frame = 0
    analysis_frames = []
    do_analysis = True

    while do_analysis and (current_frame < num_frames):
        sample_offset += size_new_data
        size_new_data = analysis_params.sizeNextRead

        frame_audio = audio[sample_offset:sample_offset + size_new_data]
        frame_audio = np.array(frame_audio, dtype=np.float32)
        if len(frame_audio) < size_new_data:
            frame_audio = np.hstack((
                frame_audio, np.zeros(size_new_data - len(frame_audio),
                                      dtype=np.float32)
            ))

        analysis_data = pysms.SMS_Data()
        pysms.sms_allocFrameH(sms_header, analysis_data)
        status = pysms.sms_analyze(frame_audio, analysis_data,
                                   analysis_params)

        analysis_frames.append(analysis_data)
        current_frame += 1

        if status == -1:
            do_analysis = False

    synth_params = _pysms_synthesis_params(sampling_rate)
    if det_synth_type == 'ifft':
        synth_params.iDetSynthType = pysms.SMS_DET_IFFT
    elif det_synth_type == 'sin':
        synth_params.iDetSynthType = pysms.SMS_DET_SIN
    else:
        raise Exception("Invalid deterministic synthesis type")

    pysms.sms_initSynth(sms_header, synth_params)

    synth_frame = np.zeros(synth_params.sizeHop, dtype=np.float32)
    synth_audio = np.array([], dtype=np.float32)

    for i in range(len(analysis_frames)):
        pysms.sms_synthesize(analysis_frames[i], synth_frame, synth_params)
        synth_audio = np.hstack((synth_audio, synth_frame))

    synth_audio = np.asarray(synth_audio * 32768, np.int16)

    for frame in analysis_frames:
        pysms.sms_freeFrame(frame)
    pysms.sms_freeAnalysis(analysis_params)
    pysms.sms_closeSF()
    pysms.sms_freeSynth(synth_params)
    pysms.sms_free()

    return synth_audio