在我的 c 代码中调用 soundtouch 代码,程序崩溃

Posted

技术标签:

【中文标题】在我的 c 代码中调用 soundtouch 代码,程序崩溃【英文标题】:call soundtouch code in my c code, the programe crashed 【发布时间】:2015-04-22 13:04:09 【问题描述】:

想在纯c代码中调用C++方法,关注了一篇文章。桥在下面:

extern "C" 
    void setSampleRateForC(SoundTouch *p, uint rate) p->setSampleRate(rate);
    void setChannelsForC(SoundTouch *p, uint channels) p->setChannels(channels);
    void setTempoChangeForC(SoundTouch *p, float tempo ) p->setTempoChange(tempo);
    void setRateChangeForC(SoundTouch *p, float rate ) p->setRateChange(rate);
    void setPitchSemiTonesForC(SoundTouch *p, float pitch) p->setPitchSemiTones(pitch);
    BOOL setSettingForC(SoundTouch *p, int settingId, int value)  return p->setSetting(settingId, value);

    void setRateForC(SoundTouch *p, uint rate) p->setRate(rate);
    void setTempoForC(SoundTouch *p, float tempo ) p->setTempo(tempo);
    void setPitchForC(SoundTouch *p, float pitch) p->setPitch(pitch);

    void putSamplesForC(SoundTouch *p,
            const SAMPLETYPE *samples,  ///< Pointer to sample buffer.
            uint numSamples                         ///< Number of samples in buffer. Notice
                                                    ///< that in case of stereo-sound a single sample
                                                    ///< contains data for both channels.
            ) p->putSamples(samples, numSamples);

    uint receiveSamplesForC(SoundTouch *p, SAMPLETYPE *output, ///< Buffer where to copy output samples.
                                uint maxSamples                 ///< How many samples to receive at max.
                                ) 
                return p->receiveSamples(output, maxSamples);
    

    SoundTouch *getInstance()
       return getInstance();
    

但是当我在我的 c 代码中调用 getIntance() 时,程序崩溃了。所以我想知道,代码有什么问题。

 LOGE("%s init SoundTouch begin", THIS_FILE);
    pSoundTouch = soundtouch::getInstance();
    LOGE("%s", THIS_FILE);

但是logcat中只发现init SoundTouch begin,而logcat中没有出现下一条日志。请帮我。谢谢你。 getInstance() 是:

SoundTouch *SoundTouch::getInstance()
    return new SoundTouch();

【问题讨论】:

【参考方案1】:

好的,我已经通过编写一个包装器解决了它:

// uses C calling convention
#include "SoundTouch.h"

#ifdef __cplusplus
extern "C" 
#endif

void* SoundTouch_Create()
    return new SoundTouch();


void SoundTouch_Destroy( void* thisC )

    delete static_cast<SoundTouch*>(thisC);


void setSampleRateForC(void* thisC , uint rate) static_cast<SoundTouch*>(thisC)->setSampleRate(rate);
    void setChannelsForC(void* thisC, uint channels) static_cast<SoundTouch*>(thisC)->setChannels(channels);
    void setTempoChangeForC(void* thisC, float tempo ) static_cast<SoundTouch*>(thisC)->setTempoChange(tempo);
    void setRateChangeForC(void* thisC, float rate ) static_cast<SoundTouch*>(thisC)->setRateChange(rate);
    void setPitchSemiTonesForC(void* thisC, float pitch) static_cast<SoundTouch*>(thisC)->setPitchSemiTones(pitch);
    BOOL setSettingForC(void* thisC, int settingId, int value)  return static_cast<SoundTouch*>(thisC)->setSetting(settingId, value);

    void setRateForC(void* thisC, uint rate) static_cast<SoundTouch*>(thisC)->setRate(rate);
    void setTempoForC(void* thisC, float tempo ) static_cast<SoundTouch*>(thisC)->setTempo(tempo);
    void setPitchForC(void* thisC, float pitch) static_cast<SoundTouch*>(thisC)->setPitch(pitch);

    void putSamplesForC(void* thisC,
            const SAMPLETYPE *samples,  ///< Pointer to sample buffer.
            uint numSamples                         ///< Number of samples in buffer. Notice
                                                    ///< that in case of stereo-sound a single sample
                                                    ///< contains data for both channels.
            ) 
                static_cast<SoundTouch*>(thisC)->putSamples(samples, numSamples);
             

    uint receiveSamplesForC(void* thisC, SAMPLETYPE *output, ///< Buffer where to copy output samples.
                                uint maxSamples                 ///< How many samples to receive at max.
                                ) 
    
        return static_cast<SoundTouch*>(thisC)->receiveSamples(output, maxSamples);
    

#ifdef __cplusplus

#endif

【讨论】:

以上是关于在我的 c 代码中调用 soundtouch 代码,程序崩溃的主要内容,如果未能解决你的问题,请参考以下文章

iOS SoundTouch 框架 BPM 检测示例

使用boost python编译在python代码内部调用的c ++代码时出错

如何在我的代码中查明触发 QObject::connect 的调用的位置:无法在 Qt5 中对类型的参数进行排队?

使用 Fortran 中的内存数据调用 C 代码

为啥在我的 java 代码中没有调用通知

如何从 C# 代码的库中调用 C 方法?