Skip to content

Instantly share code, notes, and snippets.

@popigg
Last active March 15, 2022 06:44
Show Gist options
  • Save popigg/3847a4cf71a1898e795f3fa5b8aff9a2 to your computer and use it in GitHub Desktop.
Save popigg/3847a4cf71a1898e795f3fa5b8aff9a2 to your computer and use it in GitHub Desktop.
aubio MFCC extractor swift
import Foundation
import aubio
class AubioHelper {
private let audioCapacity = 24000
private let hop_size: uint_t = 1024
private let win_size: uint_t = 4096
private let buf_size: uint_t = 4096
private let n_filters: uint_t = 40
private let n_coeffs: uint_t = 13
func getMFCC(data: [Float32]) -> [[Float32]] {
let sample_rate: uint_t = 16000
let samples = new_fvec(buf_size);
var mfcc = UnsafeMutablePointer<Float32>.allocate(capacity: audioCapacity)
let mfcc_spec = new_aubio_mfcc(buf_size, n_filters, n_coeffs, sample_rate)
let samples_cvec = new_cvec(win_size)
let pvoc = new_aubio_pvoc(win_size, hop_size)
let out = new_fvec(n_coeffs)
var sampleCount: UInt32 = 0
var total_frames : uint_t = 0
var mfcc_array: [[Float32]] = []
for i in 0..<data.count {
fvec_set_sample(samples, data[i], sampleCount)
sampleCount += 1
if sampleCount >= hop_size {
//TODO: Already tried silence threshold but didn't work
// calculate MFCCs from audio values
aubio_pvoc_do(pvoc, samples, samples_cvec)
aubio_mfcc_do(mfcc_spec, samples_cvec, out)
mfcc = fvec_get_data(out)
mfcc_array.append(Array(UnsafeBufferPointer(start: mfcc, count: Int(n_coeffs))))
total_frames += sampleCount
sampleCount = 0
}
}
// destroy used vectors
del_fvec(samples)
del_aubio_mfcc(mfcc_spec)
del_cvec(samples_cvec)
del_aubio_pvoc(pvoc)
del_fvec(out)
return mfcc_array
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment