forked from simonas-dev/aubio-go
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathonset.go
137 lines (120 loc) · 3.53 KB
/
onset.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
/*
Copyright 2013 Jeremy Wall ([email protected])
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
*/
package aubio
/*
#cgo LDFLAGS: -laubio
#include <aubio/aubio.h>
#include <aubio/onset/onset.h>
*/
import "C"
import (
"fmt"
)
type onsetMode string
const (
// Onset Detection functions see: https://github.com/piem/aubio/blob/develop/src/spectral/specdesc.h
// Energy based onset detection function
Energy onsetMode = "energy"
// High Frequency Content onset detection function
HFC onsetMode = "hfc"
// Complex Domain Method onset detection function
Complex onsetMode = "complex"
// Phase based Method onset detection function
Phase onsetMode = "phase"
// Spectral difference method onset detection function
SpecDiff onsetMode = "specdiff"
// Kullback-Liebler onset detection function
K1 onsetMode = "k1"
// Modified Kullback-Liebler onset detection function
MK1 onsetMode = "mk1"
// Spectral Flux
SpecFlux onsetMode = "specflux"
)
// Tempo is a wrapper for the aubio_tempo_t tempo detection object.
type Onset struct {
o *C.aubio_onset_t
buf *SimpleBuffer
}
// OnsetOrDie constructs a new Onset object.
// It panics on any errors.
func OnsetOrDie(mode onsetMode, bufSize, blocksize, samplerate uint) *Onset {
if t, err := NewOnset(mode, bufSize, blocksize, samplerate); err == nil {
return t
} else {
panic(err)
}
}
// NewOnset constructs a new Onset object.
// It is the Callers responsibility to call Free on the returned
// Onset object or leak memory.
// t, err := NewOnset(mode, bufSize, blockSize, samplerate)
// if err != nil {
// // handle error
// }
// defer t.Free()
func NewOnset(
onset_mode onsetMode, bufSize, blockSize, samplerate uint) (*Onset, error) {
t, err := C.new_aubio_onset(toCharTPtr(string(onset_mode)),
C.uint_t(bufSize), C.uint_t(blockSize), C.uint_t(samplerate))
if t == nil {
return nil, fmt.Errorf("failure creating Onset object %q", err)
}
return &Onset{o: t, buf: NewSimpleBuffer(blockSize)}, nil
}
func (t *Onset) Buffer() *SimpleBuffer {
return t.buf
}
// Do executes the onset detection on an input Buffer.
// It returns the estimated beat locations in a new buffer.
func (t *Onset) Do(input *SimpleBuffer) {
if t.o == nil {
return
}
C.aubio_onset_do(t.o, input.vec, t.buf.vec)
}
// SetSilence sets the onset detection silence threshold.
func (t *Onset) SetSilence(silence float64) {
if t.o == nil {
return
}
C.aubio_onset_set_silence(t.o, C.smpl_t(silence))
}
// SetThreshold sets the onset detection peak picking threshold.
func (t *Onset) SetThreshold(threshold float64) {
if t.o == nil {
return
}
C.aubio_onset_set_threshold(t.o, C.smpl_t(threshold))
}
// returns if an onset was detected in the most recent frame
func (t *Onset) OnsetNow() bool {
return C.fvec_get_sample(t.buf.vec, C.uint_t(0)) != 0
}
/* TODO(jwall): Update to the latest version of the aubio library
// GetLastOnset returns the bpm after running Do on an input Buffer
// t, err := NewOnset(mode, bufSize, blockSize, samplerate)
// if err != nil {
// }
// defer t.Close()
// t.Do(buf)
// fmt.Println("BPM: ", t.GetBpm())
func (t *Onset) GetLastOnset() float64 {
if t.o == nil {
return 0
}
return float64(C.aubio_onset_get_last_onset(t.o))
}
*/
// Free frees the aubio_temp_t object's memory.
func (t *Onset) Free() {
if t.o == nil {
return
}
C.del_aubio_onset(t.o)
t.o = nil
}