2

I am trying to access C values stored in the Aubio library and believe it's how I am accessing the Struct value.

The library has C Struct and fvec_get_data function:

typedef struct {
  uint_t length;  /**< length of buffer */
  smpl_t *data;   /**< data vector of length ::fvec_t.length */
} fvec_t;

//in fvec.c

smpl_t * fvec_get_data(const fvec_t *s) {
  return s->data;
}

Back to swift I then read in the data as suggested:

            let oout = new_fvec(n_coefs)
            let c = new_aubio_mfcc(win_s, n_filters, n_coefs, samplerate);
            var read: uint_t = 0

            var dataStore = [smpl_t]()

                while (true) {
                    aubio_source_do(b, a, &read)
                    aubio_mfcc_do(c, iin, oout)

                    dataStore.append(fvec_get_data(oout).pointee)
                    total_frames += read

                    if (read < hop_size) { break }
                }

However this doesn't retrieve all the data, in the array only the first value. In comparison in the while loop you can call:

 fvec_print(oout) // this prints out ALL values not just the first

...

Looking at the c code that does this:

void fvec_print(const fvec_t *s) {
  uint_t j;
  for (j=0; j< s->length; j++) {
    AUBIO_MSG(AUBIO_SMPL_FMT " ", s->data[j]);
  }
  AUBIO_MSG("\n");
}

Any suggestions on how to get all the values into Swift greatly appreciated.

robmsmt
  • 1,389
  • 11
  • 19

1 Answers1

2

fvec_get_data(oout) is the same as out.data, a pointer to the first element, and out.data.pointee is just the first element itself.

As in the C code you can loop over all data elements with

if let data = fvec_get_data(oout) { 
    for j in 0..<Int(n_coefs) {
        dataStore.append(data[j])
    }
}

This can be simplified by creating an UnsafeBufferPointer which is a Sequence:

if let data = fvec_get_data(oout) { 
    let buffer = UnsafeBufferPointer(start: data, count: Int(n_coefs))
    dataStore.append(contentsOf: buffer)
}
Martin R
  • 529,903
  • 94
  • 1,240
  • 1,382
  • thanks, for the first I get Value of type 'UnsafeMutablePointer?' has no member 'length'. For the second option I get Value of type 'UnsafeMutablePointer?' has no member 'data' – robmsmt Aug 21 '17 at 20:32