Пример #1
0
def analyze(audio_file, frame_rate=300, window_size=1001, window_type=pysms.SMS_WIN_HAMMING,
            num_stoc_coeffs=128, default_fundamental=100, highest_freq=12000, 
            env_type=0, env_order=0):
    pysms.sms_init()
    analysis_params = pysms.SMS_AnalParams()
    pysms.sms_initAnalParams(analysis_params)
    analysis_params.fDefaultFundamental = default_fundamental
    analysis_params.fHighestFreq = highest_freq 
    analysis_params.specEnvParams.iType = env_type 
    analysis_params.specEnvParams.iOrder = env_order
    analysis_params.iFrameRate = frame_rate
    analysis_params.nStochasticCoeff = num_stoc_coeffs

    # Try to open the input file
    snd_header = pysms.SMS_SndHeader()
    if(pysms.sms_openSF(audio_file, snd_header)):
        raise NameError("error opening sound file: " + pysms.sms_errorString())
    # initialize memory for analysis
    if pysms.sms_initAnalysis(analysis_params, snd_header) != 0:
        raise Exception("Error allocating memory for analysis_params")
    # copy data into the sms header
    sms_header = pysms.SMS_Header()
    pysms.sms_fillHeader(sms_header, analysis_params, "analysis.py")

    analysis_frames = []
    num_samples = snd_header.nSamples
    num_frames = 0
    sample_offset = 0
    size_new_data = 0
    do_analysis = True

    # Analysis loop
    while do_analysis:
        sample_offset += size_new_data
        if((sample_offset + analysis_params.sizeNextRead) < num_samples):
            size_new_data = analysis_params.sizeNextRead
        else:
            size_new_data = num_samples - sample_offset
        frame = np.zeros(size_new_data, dtype=np.float32)
        if pysms.sms_getSound(snd_header, frame, sample_offset, analysis_params):
            raise NameError("error opening sound file: " + pysms.sms_errorString())
        data = pysms.SMS_Data()
        pysms.sms_allocFrameH(sms_header, data)

        status = pysms.sms_analyze(frame, data, analysis_params)  
        if status == 1:
            analysis_frames.append(data)
            num_frames += 1
        elif status == -1:
            do_analysis = False
            sms_header.nFrames = num_frames

    # free memory and return
    pysms.sms_freeAnalysis(analysis_params)
    pysms.sms_closeSF()
    pysms.sms_free()
    return analysis_frames, sms_header, snd_header
Пример #2
0
def sms_size_next_read():
    pysms.sms_init()
    snd_header = pysms.SMS_SndHeader()

    # Try to open the input file to fill snd_header
    if(pysms.sms_openSF(audio_path, snd_header)):
        raise NameError(
            "error opening sound file: " + pysms.sms_errorString()
        )

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

    sample_offset = 0
    pysms_size_new_data = 0
    current_frame = 0
    next_read_sizes = []

    while current_frame < num_frames:
        next_read_sizes.append(analysis_params.sizeNextRead)
        sample_offset += pysms_size_new_data
        pysms_size_new_data = analysis_params.sizeNextRead

        # convert frame to floats for libsms
        frame = audio[sample_offset:sample_offset + pysms_size_new_data]
        frame = np.array(frame, dtype=np.float32)
        if len(frame) < pysms_size_new_data:
            frame = np.hstack((
                frame, np.zeros(pysms_size_new_data - len(frame),
                                dtype=np.float32)
            ))

        analysis_data = pysms.SMS_Data()
        pysms.sms_allocFrameH(sms_header, analysis_data)
        status = pysms.sms_analyze(frame, analysis_data, analysis_params)
        # as the no. of frames of delay is > num_frames, sms_analyze should
        # never get around to performing partial tracking, and so the
        # return value should be 0
        assert status == 0
        pysms.sms_freeFrame(analysis_data)
        current_frame += 1

    pysms.sms_freeAnalysis(analysis_params)
    pysms.sms_closeSF()
    pysms.sms_free()

    return next_read_sizes
Пример #3
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
Пример #4
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
Пример #5
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
Пример #6
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
Пример #7
0
def sms_partial_tracking():
    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
    sms_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)
        num_partials = analysis_data.nTracks
        status = pysms.sms_analyze(frame_audio, analysis_data,
                                   analysis_params)

        sms_freqs = np.zeros(num_partials, dtype=np.float32)
        sms_amps = np.zeros(num_partials, dtype=np.float32)
        sms_phases = np.zeros(num_partials, dtype=np.float32)

        frame = {'status': status}
        frame['partials'] = []

        if status == 1:
            analysis_data.getSinFreq(sms_freqs)
            analysis_data.getSinAmp(sms_amps)
            analysis_data.getSinPhase(sms_phases)
            current_frame += 1

        if status == -1:
            do_analysis = False

        for i in range(num_partials):
            frame['partials'].append({
                'n': i,
                'amplitude': float(sms_amps[i]),
                'frequency': float(sms_freqs[i]),
                'phase': float(sms_phases[i])
            })

        sms_frames.append(frame)
        pysms.sms_freeFrame(analysis_data)

    pysms.sms_freeAnalysis(analysis_params)
    pysms.sms_closeSF()
    pysms.sms_free()

    return sms_frames