File: Onsets.cpp

package info (click to toggle)
supercollider 1%3A3.13.0%2Brepack-1
  • links: PTS, VCS
  • area: main
  • in suites: bookworm
  • size: 80,292 kB
  • sloc: cpp: 476,363; lisp: 84,680; ansic: 77,685; sh: 25,509; python: 7,909; makefile: 3,440; perl: 1,964; javascript: 974; xml: 826; java: 677; yacc: 314; lex: 175; objc: 152; ruby: 136
file content (162 lines) | stat: -rw-r--r-- 7,109 bytes parent folder | download | duplicates (2)
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
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
/*
    Onset detector for SuperCollider
    Copyright (c) 2007 Dan Stowell. All rights reserved.
    http://onsetsds.sourceforge.net

    Now part of:

    SuperCollider real time audio synthesis system
    Copyright (c) 2002 James McCartney. All rights reserved.
    http://www.audiosynth.com

    This program is free software; you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation; either version 2 of the License, or
    (at your option) any later version.

    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with this program; if not, write to the Free Software
    Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301  USA
*/

#include "SC_PlugIn.h"
#include "SCComplex.h"
#include "FFT_UGens.h"

#include "Onsets.h"

//////////////////////////////////////////////////////////////////////////////////////////////////

// for operation on one buffer
// almost like PV_GET_BUF except it outputs unit->outval rather than -1 when FFT not triggered
#define Onsets_GET_BUF                                                                                                 \
    float fbufnum = ZIN0(0);                                                                                           \
    if (fbufnum < 0.f) {                                                                                               \
        ZOUT0(0) = unit->outval;                                                                                       \
        return;                                                                                                        \
    }                                                                                                                  \
    ZOUT0(0) = fbufnum;                                                                                                \
    uint32 ibufnum = (uint32)fbufnum;                                                                                  \
    World* world = unit->mWorld;                                                                                       \
    SndBuf* buf;                                                                                                       \
    if (ibufnum >= world->mNumSndBufs) {                                                                               \
        int localBufNum = ibufnum - world->mNumSndBufs;                                                                \
        Graph* parent = unit->mParent;                                                                                 \
        if (localBufNum <= parent->localBufNum) {                                                                      \
            buf = parent->mLocalSndBufs + localBufNum;                                                                 \
        } else {                                                                                                       \
            buf = world->mSndBufs;                                                                                     \
        }                                                                                                              \
    } else {                                                                                                           \
        buf = world->mSndBufs + ibufnum;                                                                               \
    }                                                                                                                  \
    LOCK_SNDBUF(buf);


//////////////////////////////////////////////////////////////////////////////////////////////////


////////////////////////////////////////////////////////////////////////////////////////////////////////

void Onsets_Ctor(Onsets* unit) {
    if (ZIN0(8) > 0)
        SETCALC(Onsets_next_rawodf);
    else
        SETCALC(Onsets_next);

    unit->m_needsinit = true;
    unit->m_ods = (OnsetsDS*)RTAlloc(unit->mWorld, sizeof(OnsetsDS));
    unit->m_odsdata = nullptr;
    ClearUnitIfMemFailed(unit->m_ods);

    ZOUT0(0) = unit->outval = 0.f;
}

void Onsets_next(Onsets* unit, int inNumSamples) {
    Onsets_GET_BUF

        // In practice, making the polar conversion here in SC is more efficient because SC provides a lookup table
        // method.
        SCPolarBuf* p = ToPolarApx(buf);

    OnsetsDS* ods = unit->m_ods;

    int odftype = (int)ZIN0(2);
    float relaxtime = ZIN0(3);
    int medspan = (int)ZIN0(6);

    if (unit->m_needsinit) {
        // Init happens here because we need to be sure about FFT size.
        unit->m_odsdata = (float*)RTAlloc(unit->mWorld, onsetsds_memneeded(odftype, buf->samples, medspan));
        ClearUnitIfMemFailed(unit->m_odsdata);

        onsetsds_init(ods, unit->m_odsdata, ODS_FFT_SC3_POLAR, odftype, buf->samples, medspan, FULLRATE);
        onsetsds_setrelax(ods, relaxtime, buf->samples >> 1);

        unit->m_needsinit = false;
    }

    // Here is the best place to set parameters - after init is ensured
    // These are "painless" to set:
    ods->thresh = ZIN0(1);
    ods->floor = ZIN0(4);
    ods->mingap = (int)ZIN0(5);
    ods->whtype = (int)ZIN0(7);

    // Now to process
    unit->outval = onsetsds_process(ods, (float*)p);

    ZOUT0(0) = unit->outval;
}

void Onsets_next_rawodf(Onsets* unit, int inNumSamples) {
    Onsets_GET_BUF

        // In practice, making the polar conversion here in SC is more efficient because SC provides a lookup table
        // method.
        SCPolarBuf* p = ToPolarApx(buf);

    OnsetsDS* ods = unit->m_ods;

    int odftype = (int)ZIN0(2);
    float relaxtime = ZIN0(3);
    int medspan = (int)ZIN0(6);

    if (unit->m_needsinit) {
        // Init happens here because we need to be sure about FFT size.
        unit->m_odsdata = (float*)RTAlloc(unit->mWorld, onsetsds_memneeded(odftype, buf->samples, medspan));
        ClearUnitIfMemFailed(unit->m_odsdata);

        onsetsds_init(ods, unit->m_odsdata, ODS_FFT_SC3_POLAR, odftype, buf->samples, medspan, FULLRATE);
        onsetsds_setrelax(ods, relaxtime, buf->samples >> 1);

        unit->m_needsinit = false;
    }

    // Here is the best place to set parameters - after init is ensured
    // These are "painless" to set:
    ods->thresh = ZIN0(1);
    ods->floor = ZIN0(4);
    ods->mingap = (int)ZIN0(5);
    ods->whtype = (int)ZIN0(7);

    // Now to process
    onsetsds_process(ods, (float*)p);
    // But we want the ODF, not the triggers, for this special mode...
    // unit->outval = ods->odfvalpost;
    unit->outval = ods->odfvals[0];

    ZOUT0(0) = unit->outval;
}

void Onsets_Dtor(Onsets* unit) {
    if (!unit->m_needsinit) {
        RTFree(unit->mWorld, unit->m_odsdata);
    }
    RTFree(unit->mWorld, unit->m_ods);
}