1 | |
---|
2 | |
---|
3 | #include "aubio_priv.h" |
---|
4 | |
---|
5 | #ifdef HAVE_RUBBERBAND |
---|
6 | |
---|
7 | #include <rubberband/rubberband-c.h> |
---|
8 | |
---|
9 | // check rubberband is 1.8.1, warn if 1.3 |
---|
10 | #if !((RUBBERBAND_API_MAJOR_VERSION >= 2) && \ |
---|
11 | (RUBBERBAND_API_MINOR_VERSION >= 5)) |
---|
12 | #warning RubberBandOptionDetectorSoft not available, \ |
---|
13 | please upgrade rubberband to version 1.8.1 or higher |
---|
14 | #define RubberBandOptionDetectorSoft 0x00000000 |
---|
15 | #endif |
---|
16 | |
---|
17 | #include <stdio.h> |
---|
18 | #include <stdlib.h> |
---|
19 | #include <string.h> |
---|
20 | |
---|
21 | char_t** aubio_split_str(const char_t* str, const char_t sep) { |
---|
22 | char_t** result = 0; |
---|
23 | uint_t count = 0; |
---|
24 | char_t input[PATH_MAX]; |
---|
25 | char_t* in_ptr = input; |
---|
26 | char_t* last_sep = 0; |
---|
27 | char_t delim[2]; delim[0] = sep; delim[1] = 0; |
---|
28 | |
---|
29 | strncpy(input, str, PATH_MAX); |
---|
30 | input[PATH_MAX - 1] = '\0'; |
---|
31 | |
---|
32 | // count number of elements |
---|
33 | while (*in_ptr) { |
---|
34 | if (sep == *in_ptr) { |
---|
35 | count++; |
---|
36 | last_sep = in_ptr; |
---|
37 | } |
---|
38 | in_ptr++; |
---|
39 | } |
---|
40 | // add space for trailing token. |
---|
41 | count += last_sep < (input + strlen(input) - 1); |
---|
42 | count++; |
---|
43 | |
---|
44 | result = AUBIO_ARRAY(char_t*, count); |
---|
45 | if (result) { |
---|
46 | uint_t idx = 0; |
---|
47 | char_t* params = strtok(input, delim); |
---|
48 | while (params) { |
---|
49 | // make sure we don't got in the wild |
---|
50 | if (idx >= count) |
---|
51 | break; |
---|
52 | *(result + idx++) = strdup(params); |
---|
53 | params = strtok(0, delim); |
---|
54 | } |
---|
55 | // add null string at the end if needed |
---|
56 | if (idx < count - 1) |
---|
57 | *(result + idx) = 0; |
---|
58 | } |
---|
59 | return result; |
---|
60 | } |
---|
61 | |
---|
62 | RubberBandOptions aubio_get_rubberband_opts(const char_t *mode) |
---|
63 | { |
---|
64 | RubberBandOptions rboptions = RubberBandOptionProcessRealTime; |
---|
65 | |
---|
66 | if ( strcmp(mode,"crispness:0") == 0 ) { |
---|
67 | rboptions |= RubberBandOptionTransientsSmooth; |
---|
68 | rboptions |= RubberBandOptionWindowLong; |
---|
69 | rboptions |= RubberBandOptionPhaseIndependent; |
---|
70 | } else if ( strcmp(mode, "crispness:1") == 0 ) { |
---|
71 | rboptions |= RubberBandOptionDetectorSoft; |
---|
72 | rboptions |= RubberBandOptionTransientsSmooth; |
---|
73 | rboptions |= RubberBandOptionWindowLong; |
---|
74 | rboptions |= RubberBandOptionPhaseIndependent; |
---|
75 | } else if ( strcmp(mode, "crispness:2") == 0 ) { |
---|
76 | rboptions |= RubberBandOptionTransientsSmooth; |
---|
77 | rboptions |= RubberBandOptionPhaseIndependent; |
---|
78 | } else if ( strcmp(mode, "crispness:3") == 0 ) { |
---|
79 | rboptions |= RubberBandOptionTransientsSmooth; |
---|
80 | } else if ( strcmp(mode, "crispness:4") == 0 ) { |
---|
81 | // same as "default" |
---|
82 | } else if ( strcmp(mode, "crispness:5") == 0 ) { |
---|
83 | rboptions |= RubberBandOptionTransientsCrisp; |
---|
84 | } else if ( strcmp(mode, "crispness:6") == 0 ) { |
---|
85 | rboptions |= RubberBandOptionTransientsCrisp; |
---|
86 | rboptions |= RubberBandOptionWindowShort; |
---|
87 | rboptions |= RubberBandOptionPhaseIndependent; |
---|
88 | } else if ( strcmp(mode, "default") == 0 ) { |
---|
89 | // nothing to do |
---|
90 | } else { |
---|
91 | // attempt to parse a list of options, separated with ',' |
---|
92 | char_t **params = aubio_split_str(mode, ':'); |
---|
93 | uint_t i = 0; |
---|
94 | if (!params || !params[0]) { |
---|
95 | // memory failure occurred or empty string was passed |
---|
96 | AUBIO_ERR("rubberband_utils: failed parsing options\n"); |
---|
97 | rboptions = -1; |
---|
98 | } |
---|
99 | while (*(params + i) != NULL) { |
---|
100 | if ( strcmp(params[i], "ProcessOffline" ) == 0 ) { |
---|
101 | rboptions = RubberBandOptionProcessOffline; |
---|
102 | // TODO: add wrapper to rb study(smpl_t *input, uint_t write) |
---|
103 | AUBIO_ERR("rubberband_utils: RubberBandOptionProcessOffline is not available\n"); |
---|
104 | rboptions = -1; |
---|
105 | } |
---|
106 | else if ( strcmp(params[i], "ProcessRealTime" ) == 0 ) rboptions |= RubberBandOptionProcessRealTime; |
---|
107 | else if ( strcmp(params[i], "StretchElastic" ) == 0 ) rboptions |= RubberBandOptionStretchElastic; |
---|
108 | else if ( strcmp(params[i], "StretchPrecise" ) == 0 ) rboptions |= RubberBandOptionStretchPrecise; |
---|
109 | else if ( strcmp(params[i], "TransientsCrisp" ) == 0 ) rboptions |= RubberBandOptionTransientsCrisp; |
---|
110 | else if ( strcmp(params[i], "TransientsMixed" ) == 0 ) rboptions |= RubberBandOptionTransientsMixed; |
---|
111 | else if ( strcmp(params[i], "TransientsSmooth" ) == 0 ) rboptions |= RubberBandOptionTransientsSmooth; |
---|
112 | else if ( strcmp(params[i], "DetectorCompound" ) == 0 ) rboptions |= RubberBandOptionDetectorCompound; |
---|
113 | else if ( strcmp(params[i], "DetectorPercussive" ) == 0 ) rboptions |= RubberBandOptionDetectorPercussive; |
---|
114 | else if ( strcmp(params[i], "DetectorSoft" ) == 0 ) rboptions |= RubberBandOptionDetectorSoft; |
---|
115 | else if ( strcmp(params[i], "PhaseLaminar" ) == 0 ) rboptions |= RubberBandOptionPhaseLaminar; |
---|
116 | else if ( strcmp(params[i], "PhaseIndependent" ) == 0 ) rboptions |= RubberBandOptionPhaseIndependent; |
---|
117 | else if ( strcmp(params[i], "ThreadingAuto" ) == 0 ) rboptions |= RubberBandOptionThreadingAuto; |
---|
118 | else if ( strcmp(params[i], "ThreadingNever" ) == 0 ) rboptions |= RubberBandOptionThreadingNever; |
---|
119 | else if ( strcmp(params[i], "ThreadingAlways" ) == 0 ) rboptions |= RubberBandOptionThreadingAlways; |
---|
120 | else if ( strcmp(params[i], "WindowStandard" ) == 0 ) rboptions |= RubberBandOptionWindowStandard; |
---|
121 | else if ( strcmp(params[i], "WindowShort" ) == 0 ) rboptions |= RubberBandOptionWindowShort; |
---|
122 | else if ( strcmp(params[i], "WindowLong" ) == 0 ) rboptions |= RubberBandOptionWindowLong; |
---|
123 | else if ( strcmp(params[i], "SmoothingOff" ) == 0 ) rboptions |= RubberBandOptionSmoothingOff; |
---|
124 | else if ( strcmp(params[i], "SmoothingOn" ) == 0 ) rboptions |= RubberBandOptionSmoothingOn; |
---|
125 | else if ( strcmp(params[i], "FormantShifted" ) == 0 ) rboptions |= RubberBandOptionFormantShifted; |
---|
126 | else if ( strcmp(params[i], "FormantPreserved" ) == 0 ) rboptions |= RubberBandOptionFormantPreserved; |
---|
127 | else if ( strcmp(params[i], "PitchHighSpeed" ) == 0 ) rboptions |= RubberBandOptionPitchHighSpeed; |
---|
128 | else if ( strcmp(params[i], "PitchHighQuality" ) == 0 ) rboptions |= RubberBandOptionPitchHighQuality; |
---|
129 | else if ( strcmp(params[i], "PitchHighConsistency" ) == 0 ) rboptions |= RubberBandOptionPitchHighConsistency; |
---|
130 | else if ( strcmp(params[i], "ChannelsApart" ) == 0 ) rboptions |= RubberBandOptionChannelsApart; |
---|
131 | else if ( strcmp(params[i], "ChannelsTogether" ) == 0 ) rboptions |= RubberBandOptionChannelsTogether; |
---|
132 | else { |
---|
133 | AUBIO_ERR("rubberband_utils: did not understand option '%s', should be one of: " |
---|
134 | "StretchElastic|StretchPrecise, TransientsCrisp|TransientsMixed|TransientsSmooth, " |
---|
135 | "DetectorCompound|DetectorPercussive|DetectorSoft, PhaseLaminar|PhaseIndependent, " |
---|
136 | "ThreadingAuto|ThreadingNever|ThreadingAlways, WindowStandard|WindowLong|WindowShort, " |
---|
137 | "SmoothingOn|SmoothingOff, FormantShifted|FormantPreserved, " |
---|
138 | "PitchHighSpeed|PitchHighQuality|PitchHighConsistency, ChannelsApart|ChannelsTogether\n" |
---|
139 | , params[i]); |
---|
140 | rboptions = -1; |
---|
141 | } |
---|
142 | AUBIO_FREE(params[i]); |
---|
143 | i++; |
---|
144 | } |
---|
145 | AUBIO_FREE(params); |
---|
146 | } |
---|
147 | return rboptions; |
---|
148 | } |
---|
149 | |
---|
150 | #endif |
---|