spacepaste

  1.  
  2. from cffi import FFI
  3. ffibuilder = FFI()
  4. ffibuilder.set_source("selfsample",
  5. r"""
  6. #include <alsa/asoundlib.h>
  7. #include <math.h>
  8. #include <errno.h>
  9. #include <unistd.h>
  10. snd_pcm_t *out_handle;
  11. snd_pcm_t *in_handle;
  12. snd_pcm_stream_t out_stream = SND_PCM_STREAM_PLAYBACK;
  13. snd_pcm_stream_t in_stream = SND_PCM_STREAM_CAPTURE;
  14. snd_pcm_hw_params_t *out_params;
  15. snd_pcm_hw_params_t *in_params;
  16. char *pcm_out_name;
  17. char *pcm_in_name;
  18. #define PERIOD_SIZE 8192
  19. #define SAMPLE_RATE 48000
  20. snd_pcm_t *open_output_stream();
  21. """,
  22. libraries=[])
  23. ffibuilder.cdef(r"""
  24. typedef int... snd_pcm_uframes_t;
  25. struct snd_pcm_t *
  26. open_output_stream()
  27. {
  28. int rate = 48000; /* Sample rate */
  29. int exact_rate; /* Sample rate returned by */
  30. /* snd_pcm_hw_params_set_rate_near */
  31. int dir; /* exact_rate == rate --> dir = 0 */
  32. /* exact_rate < rate --> dir = -1 */
  33. /* exact_rate > rate --> dir = 1 */
  34. int periods = 2; /* Number of periods */
  35. snd_pcm_uframes_t periodsize = PERIOD_SIZE; /* Periodsize (bytes) */
  36. snd_pcm_t *handle;
  37. if (snd_pcm_open(&handle, pcm_out_name, out_stream, SND_PCM_NONBLOCK) < 0) {
  38. fprintf(stderr, "Error opening PCM device %s\n", pcm_out_name);
  39. return(NULL);
  40. }
  41. if (snd_pcm_set_params(handle,
  42. SND_PCM_FORMAT_S16_LE,
  43. SND_PCM_ACCESS_RW_INTERLEAVED,
  44. 1,
  45. 48000,
  46. 1,
  47. 50000) < 0) { /* 0.5sec */
  48. return(NULL);
  49. }
  50. return handle;
  51. }
  52. """)
  53. Traceback (most recent call last):
  54. File "selfsample_build.py", line 86, in <module>
  55. """)
  56. File "/usr/lib64/python3.6/site-packages/cffi/api.py", line 106, in cdef
  57. self._cdef(csource, override=override, packed=packed)
  58. File "/usr/lib64/python3.6/site-packages/cffi/api.py", line 120, in _cdef
  59. self._parser.parse(csource, override=override, **options)
  60. File "/usr/lib64/python3.6/site-packages/cffi/cparser.py", line 308, in parse
  61. self._internal_parse(csource)
  62. File "/usr/lib64/python3.6/site-packages/cffi/cparser.py", line 351, in _internal_parse
  63. raise CDefError("unrecognized construct", decl)
  64. cffi.error.CDefError: line 5: unrecognized construct
  65.