Debian Codegraph @

examples/libopenmpt_example_c_pipe.c

raw
1 /*
2 * libopenmpt_example_c_pipe.c
3 * ---------------------------
4 * Purpose: libopenmpt C API simple pipe example
5 * Notes : This example writes raw 48000Hz / stereo / 16bit native endian PCM data to stdout.
6 * Authors: OpenMPT Devs
7 * The OpenMPT source code is released under the BSD license. Read LICENSE for more details.
8 */
9
10 /*
11 * Usage: cat SOMEMODULE | libopenmpt_example_c_pipe | aplay --file-type raw --format=dat
12 */
13
14 #include <memory.h>
15 #include <stdint.h>
16 #include <stdlib.h>
17 #include <string.h>
18
19 #include <errno.h>
20 #include <unistd.h>
21
22 #include <libopenmpt/libopenmpt.h>
23 #include <libopenmpt/libopenmpt_stream_callbacks_fd.h>
24
25 #if defined( __DJGPP__ )
26 #include <crt0.h>
27 #endif /* __DJGPP__ */
28
29 #define BUFFERSIZE 480
30 #define SAMPLERATE 48000
31
32 static void libopenmpt_example_logfunc( const char * message, void * userdata ) {
33 (void)userdata;
34 if ( message ) {
35 fprintf( stderr, "openmpt: %s\n", message );
36 }
37 }
38
39 static int libopenmpt_example_errfunc( int error, void * userdata ) {
40 (void)userdata;
41 (void)error;
42 return OPENMPT_ERROR_FUNC_RESULT_DEFAULT & ~OPENMPT_ERROR_FUNC_RESULT_LOG;
43 }
44
45 static void libopenmpt_example_print_error( const char * func_name, int mod_err, const char * mod_err_str ) {
46 if ( !func_name ) {
47 func_name = "unknown function";
48 }
49 if ( mod_err == OPENMPT_ERROR_OUT_OF_MEMORY ) {
50 mod_err_str = openmpt_error_string( mod_err );
51 if ( !mod_err_str ) {
52 fprintf( stderr, "Error: %s\n", "OPENMPT_ERROR_OUT_OF_MEMORY" );
53 } else {
54 fprintf( stderr, "Error: %s\n", mod_err_str );
55 openmpt_free_string( mod_err_str );
56 mod_err_str = NULL;
57 }
58 } else {
59 if ( !mod_err_str ) {
60 mod_err_str = openmpt_error_string( mod_err );
61 if ( !mod_err_str ) {
62 fprintf( stderr, "Error: %s failed.\n", func_name );
63 } else {
64 fprintf( stderr, "Error: %s failed: %s\n", func_name, mod_err_str );
65 }
66 openmpt_free_string( mod_err_str );
67 mod_err_str = NULL;
68 }
69 fprintf( stderr, "Error: %s failed: %s\n", func_name, mod_err_str );
70 }
71 }
72
73 static ssize_t xwrite( int fd, const void * buffer, size_t size ) {
74 size_t written = 0;
75 ssize_t retval = 0;
76 while ( written < size ) {
77 retval = write( fd, (const char *)buffer + written, size - written );
78 if ( retval < 0 ) {
79 if ( errno != EINTR ) {
80 break;
81 }
82 retval = 0;
83 }
84 written += retval;
85 }
86 return written;
87 }
88
89 static int16_t buffer[BUFFERSIZE * 2];
90
91 #if defined( __DJGPP__ )
92 /* clang-format off */
93 int _crt0_startup_flags = 0
94 | _CRT0_FLAG_NONMOVE_SBRK /* force interrupt compatible allocation */
95 | _CRT0_DISABLE_SBRK_ADDRESS_WRAP /* force NT compatible allocation */
96 | _CRT0_FLAG_LOCK_MEMORY /* lock all code and data at program startup */
97 | 0;
98 /* clang-format on */
99 #endif /* __DJGPP__ */
100 #if ( defined( _WIN32 ) || defined( WIN32 ) ) && ( defined( _UNICODE ) || defined( UNICODE ) )
101 #if defined( __clang__ ) && !defined( _MSC_VER )
102 int wmain( int argc, wchar_t * argv[] );
103 #endif
104 int wmain( int argc, wchar_t * argv[] ) {
105 #else
106 int main( int argc, char * argv[] ) {
107 #endif
108 #if defined( __DJGPP__ )
109 /* clang-format off */
110 _crt0_startup_flags &= ~_CRT0_FLAG_LOCK_MEMORY; /* disable automatic locking for all further memory allocations */
111 /* clang-format on */
112 #endif /* __DJGPP__ */
113
114 int result = 0;
115 openmpt_module * mod = 0;
116 int mod_err = OPENMPT_ERROR_OK;
117 const char * mod_err_str = NULL;
118 size_t count = 0;
119 size_t written = 0;
120 (void)argv;
121
122 if ( argc != 1 ) {
123 fprintf( stderr, "Error: %s\n", "Wrong invocation. Use 'libopenmpt_example_c_pipe' and connect stdin and stdout." );
124 goto fail;
125 }
126
127 mod = openmpt_module_create2( openmpt_stream_get_fd_callbacks(), (void *)(uintptr_t)STDIN_FILENO, &libopenmpt_example_logfunc, NULL, &libopenmpt_example_errfunc, NULL, &mod_err, &mod_err_str, NULL );
128 if ( !mod ) {
129 libopenmpt_example_print_error( "openmpt_module_create2()", mod_err, mod_err_str );
130 openmpt_free_string( mod_err_str );
131 mod_err_str = NULL;
132 goto fail;
133 }
134
135 while ( 1 ) {
136
137 openmpt_module_error_clear( mod );
138 count = openmpt_module_read_interleaved_stereo( mod, SAMPLERATE, BUFFERSIZE, buffer );
139 mod_err = openmpt_module_error_get_last( mod );
140 mod_err_str = openmpt_module_error_get_last_message( mod );
141 if ( mod_err != OPENMPT_ERROR_OK ) {
142 libopenmpt_example_print_error( "openmpt_module_read_interleaved_stereo()", mod_err, mod_err_str );
143 openmpt_free_string( mod_err_str );
144 mod_err_str = NULL;
145 }
146 if ( count == 0 ) {
147 break;
148 }
149
150 written = xwrite( STDOUT_FILENO, buffer, count * 2 * sizeof( int16_t ) );
151 if ( written == 0 ) {
152 fprintf( stderr, "Error: %s\n", "write() failed." );
153 goto fail;
154 }
155 }
156
157 result = 0;
158
159 goto cleanup;
160
161 fail:
162
163 result = 1;
164
165 cleanup:
166
167 if ( mod ) {
168 openmpt_module_destroy( mod );
169 mod = 0;
170 }
171
172 return result;
173 }