libopenmpt_example_c_unsafe.c 1.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768
  1. /*
  2. * libopenmpt_example_c_unsafe.c
  3. * -----------------------------
  4. * Purpose: libopenmpt C API simplified example
  5. * Notes : PortAudio is used for sound output.
  6. * Authors: OpenMPT Devs
  7. * The OpenMPT source code is released under the BSD license. Read LICENSE for more details.
  8. */
  9. /*
  10. * Usage: libopenmpt_example_c_unsafe SOMEMODULE
  11. * CAUTION: This simple example does no error cheking at all.
  12. */
  13. #include <memory.h>
  14. #include <stdint.h>
  15. #include <stdio.h>
  16. #include <stdlib.h>
  17. #include <string.h>
  18. #include <libopenmpt/libopenmpt.h>
  19. #include <libopenmpt/libopenmpt_stream_callbacks_file.h>
  20. #include <portaudio.h>
  21. #define BUFFERSIZE 480
  22. #define SAMPLERATE 48000
  23. static int16_t left[BUFFERSIZE];
  24. static int16_t right[BUFFERSIZE];
  25. static int16_t * const buffers[2] = { left, right };
  26. #if ( defined( _WIN32 ) || defined( WIN32 ) ) && ( defined( _UNICODE ) || defined( UNICODE ) )
  27. #if defined( __clang__ ) && !defined( _MSC_VER )
  28. int wmain( int argc, wchar_t * argv[] );
  29. #endif
  30. int wmain( int argc, wchar_t * argv[] ) {
  31. #else
  32. int main( int argc, char * argv[] ) {
  33. #endif
  34. FILE * file = 0;
  35. openmpt_module * mod = 0;
  36. size_t count = 0;
  37. PaStream * stream = 0;
  38. (void)argc;
  39. #if ( defined( _WIN32 ) || defined( WIN32 ) ) && ( defined( _UNICODE ) || defined( UNICODE ) )
  40. file = _wfopen( argv[1], L"rb" );
  41. #else
  42. file = fopen( argv[1], "rb" );
  43. #endif
  44. mod = openmpt_module_create2( openmpt_stream_get_file_callbacks(), file, NULL, NULL, NULL, NULL, NULL, NULL, NULL );
  45. fclose( file );
  46. Pa_Initialize();
  47. Pa_OpenDefaultStream( &stream, 0, 2, paInt16 | paNonInterleaved, SAMPLERATE, paFramesPerBufferUnspecified, NULL, NULL );
  48. Pa_StartStream( stream );
  49. while ( 1 ) {
  50. count = openmpt_module_read_stereo( mod, SAMPLERATE, BUFFERSIZE, left, right );
  51. if ( count == 0 ) {
  52. break;
  53. }
  54. Pa_WriteStream( stream, buffers, (unsigned long)count );
  55. }
  56. Pa_StopStream( stream );
  57. Pa_CloseStream( stream );
  58. Pa_Terminate();
  59. openmpt_module_destroy( mod );
  60. return 0;
  61. }