Use a .wav file in C++ in Xcode

Viewed 72

I'm trying to read a .wav file from my Xcode files to use it in a C++ function that is located in a C++ library inside my Xcode project. I don't know where do I need to put the file (in which group or even in which target) so the C++ function can read it. I also need to call the function from swift because it is an iOS project. This is the two C++ functions that use the file:

EXPORT
string getMark()
{
  std::string result;
  result = get_watermark("piano2.wav", "");

  return (result.c_str());
}

This is the other one:

std::string get_watermark (const string& infile, const string& orig_pattern)
{
    std::string patterns;
    WavData wav_data;
    Error err = wav_data.load (infile);
    if (err)
    {
        error ("audiowmark: error loading %s: %s\n", infile.c_str(), err.message());
        return "1";
    }

    if (Params::test_truncate)
    {
        const size_t  want_n_samples = wav_data.sample_rate() * wav_data.n_channels() * Params::test_truncate;
        vector<float> short_samples  = wav_data.samples();

        if (want_n_samples < short_samples.size())
        {
            short_samples.resize (want_n_samples);
            wav_data.set_samples (short_samples);
        }
    }

    int result;
    if (wav_data.sample_rate() == Params::mark_sample_rate)
    {
        result = decode_and_report (wav_data, orig_pattern);
        
        if (PatternStorage::getInstance().getPatterns() != "") {
            patterns = PatternStorage::getInstance().getPatterns();
        }

        return patterns;
    }
    else
    {
        result = decode_and_report (resample (wav_data, Params::mark_sample_rate), orig_pattern);
        
        if (PatternStorage::getInstance().getPatterns() != "") {
            patterns = PatternStorage::getInstance().getPatterns();
        }

        return patterns;
    }
}

This is an example of how I call a C++ function from the actual swift project.

cpp file:

#define EXPORT __attribute__((visibility("default")))

EXPORT
string get_wm_string () {
  string hello = "Hello from C++";
  return hello;
}

.h file:

extern const char* get_wm_string(void);

@interface WatermarkLib : NSObject

- (void)getCPPString;

@end

.mm file:

@implementation WatermarkLib

- (void)getCPPString {
  NSString* externalString = [NSString stringWithCString:get_wm_string() encoding:[NSString defaultCStringEncoding]];
  NSLog(@"Exito! %@", externalString);
}

@end

This is how the working directory looks like from Xcode:

enter image description here

enter image description here

0 Answers
Related