@@ -29,93 +29,113 @@ extern "C" {
29
29
to the speakers, with minimal latency and the proper sample-rate for the
30
30
platform.
31
31
32
- @code
33
- cubeb * app_ctx;
34
- cubeb_init(&app_ctx, "Example Application", NULL);
35
- int rv;
36
- uint32_t rate;
37
- uint32_t latency_frames;
38
- uint64_t ts;
39
-
40
- rv = cubeb_get_preferred_sample_rate(app_ctx, &rate);
41
- if (rv != CUBEB_OK) {
42
- fprintf(stderr, "Could not get preferred sample-rate");
43
- return rv;
44
- }
45
-
46
- cubeb_stream_params output_params;
47
- output_params.format = CUBEB_SAMPLE_FLOAT32NE;
48
- output_params.rate = rate;
49
- output_params.channels = 2;
50
- output_params.layout = CUBEB_LAYOUT_UNDEFINED;
51
- output_params.prefs = CUBEB_STREAM_PREF_NONE;
52
-
53
- rv = cubeb_get_min_latency(app_ctx, &output_params, &latency_frames);
54
- if (rv != CUBEB_OK) {
55
- fprintf(stderr, "Could not get minimum latency");
56
- return rv;
57
- }
58
-
59
- cubeb_stream_params input_params;
60
- input_params.format = CUBEB_SAMPLE_FLOAT32NE;
61
- input_params.rate = rate;
62
- input_params.channels = 1;
63
- input_params.layout = CUBEB_LAYOUT_UNDEFINED;
64
- input_params.prefs = CUBEB_STREAM_PREF_NONE;
65
-
66
- cubeb_stream * stm;
67
- rv = cubeb_stream_init(app_ctx, &stm, "Example Stream 1",
68
- NULL, &input_params,
69
- NULL, &output_params,
70
- latency_frames,
71
- data_cb, state_cb,
72
- NULL);
73
- if (rv != CUBEB_OK) {
74
- fprintf(stderr, "Could not open the stream");
75
- return rv;
76
- }
77
-
78
- rv = cubeb_stream_start(stm);
79
- if (rv != CUBEB_OK) {
80
- fprintf(stderr, "Could not start the stream");
81
- return rv;
82
- }
83
- for (;;) {
84
- cubeb_stream_get_position(stm, &ts);
85
- printf("time=%llu\n", ts);
86
- sleep(1);
87
- }
88
- rv = cubeb_stream_stop(stm);
89
- if (rv != CUBEB_OK) {
90
- fprintf(stderr, "Could not stop the stream");
91
- return rv;
92
- }
93
-
94
- cubeb_stream_destroy(stm);
95
- cubeb_destroy(app_ctx);
96
- @endcode
32
+ If you receive "Could not open the stream", you likely do not have a
33
+ microphone. In that case, change PIPE_INPUT_TO_OUTPUT from true to false.
97
34
98
35
@code
36
+ #include "stdio.h"
37
+ #include "cubeb/cubeb.h"
38
+ #ifdef _WIN32
39
+ #define WIN32_LEAN_AND_MEAN
40
+ #include <Windows.h>
41
+ #include <Objbase.h>
42
+ #else
43
+ #include <unistd.h>
44
+ #endif
45
+ #define PIPE_INPUT_TO_OUTPUT true
46
+
99
47
long data_cb(cubeb_stream * stm, void * user,
100
48
const void * input_buffer, void * output_buffer, long nframes)
101
49
{
102
- const float * in = input_buffer;
103
- float * out = output_buffer;
104
-
50
+ const float * in = (float *)input_buffer;
51
+ float * out = (float *)output_buffer;
105
52
for (int i = 0; i < nframes; ++i) {
106
53
for (int c = 0; c < 2; ++c) {
54
+ #if PIPE_INPUT_TO_OUTPUT
107
55
out[2 * i + c] = in[i];
56
+ #else
57
+ out[2 * i + c] = (float)((i * 123) % 200) / 10000.f;
58
+ #endif
108
59
}
109
60
}
110
61
return nframes;
111
62
}
112
- @endcode
113
63
114
- @code
115
64
void state_cb(cubeb_stream * stm, void * user, cubeb_state state)
116
65
{
117
66
printf("state=%d\n", state);
118
67
}
68
+
69
+
70
+ int main() {
71
+ #if _WIN32
72
+ CoInitializeEx(NULL, COINIT_APARTMENTTHREADED);
73
+ #endif
74
+ cubeb * app_ctx;
75
+ cubeb_init(&app_ctx, "Example Application", NULL);
76
+ int rv;
77
+ uint32_t rate;
78
+ uint32_t latency_frames;
79
+ uint64_t ts;
80
+ rv = cubeb_get_preferred_sample_rate(app_ctx, &rate);
81
+ if (rv != CUBEB_OK) {
82
+ fprintf(stderr, "Could not get preferred sample-rate");
83
+ return rv;
84
+ }
85
+ cubeb_stream_params output_params;
86
+ output_params.format = CUBEB_SAMPLE_FLOAT32NE;
87
+ output_params.rate = rate;
88
+ output_params.channels = 2;
89
+ output_params.layout = CUBEB_LAYOUT_UNDEFINED;
90
+ output_params.prefs = CUBEB_STREAM_PREF_NONE;
91
+ rv = cubeb_get_min_latency(app_ctx, &output_params, &latency_frames);
92
+ if (rv != CUBEB_OK) {
93
+ fprintf(stderr, "Could not get minimum latency");
94
+ return rv;
95
+ }
96
+ cubeb_stream_params input_params;
97
+ input_params.format = CUBEB_SAMPLE_FLOAT32NE;
98
+ input_params.rate = rate;
99
+ input_params.channels = 1;
100
+ input_params.layout = CUBEB_LAYOUT_UNDEFINED;
101
+ input_params.prefs = CUBEB_STREAM_PREF_NONE;
102
+ cubeb_stream * stm;
103
+ rv = cubeb_stream_init(app_ctx, &stm, "Example Stream 1",
104
+ #if PIPE_INPUT_TO_OUTPUT
105
+ NULL, &input_params,
106
+ #else
107
+ NULL, NULL,
108
+ #endif
109
+ NULL, &output_params,
110
+ latency_frames,
111
+ data_cb, state_cb,
112
+ NULL);
113
+ if (rv != CUBEB_OK) {
114
+ fprintf(stderr, "Could not open the stream");
115
+ return rv;
116
+ }
117
+ rv = cubeb_stream_start(stm);
118
+ if (rv != CUBEB_OK) {
119
+ fprintf(stderr, "Could not start the stream");
120
+ return rv;
121
+ }
122
+ for (;;) {
123
+ cubeb_stream_get_position(stm, &ts);
124
+ printf("time=%llu\n", ts);
125
+ #if _WIN32
126
+ Sleep(1);
127
+ #else
128
+ sleep(1);
129
+ #endif
130
+ }
131
+ rv = cubeb_stream_stop(stm);
132
+ if (rv != CUBEB_OK) {
133
+ fprintf(stderr, "Could not stop the stream");
134
+ return rv;
135
+ }
136
+ cubeb_stream_destroy(stm);
137
+ cubeb_destroy(app_ctx);
138
+ }
119
139
@endcode
120
140
*/
121
141
0 commit comments