movie_audio_rec.c 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  1. /* -*- c-file-style: "linux" -*- */
  2. /***
  3. This file is part of PulseAudio.
  4. PulseAudio is free software; you can redistribute it and/or modify
  5. it under the terms of the GNU Lesser General Public License as published
  6. by the Free Software Foundation; either version 2.1 of the License,
  7. or (at your option) any later version.
  8. PulseAudio is distributed in the hope that it will be useful, but
  9. WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  11. General Public License for more details.
  12. You should have received a copy of the GNU Lesser General Public License
  13. along with PulseAudio; if not, see <http://www.gnu.org/licenses/>.
  14. * Copyright 2022, 2024 Pavel Machek
  15. ***/
  16. #include <stdio.h>
  17. #include <unistd.h>
  18. #include <string.h>
  19. #include <errno.h>
  20. #include <fcntl.h>
  21. #include <stdlib.h>
  22. #include <pulse/simple.h>
  23. #include <pulse/error.h>
  24. #include "medianame.h"
  25. /* gcc xx.c -o xx $(pkg-config --cflags --libs libpulse-simple)
  26. */
  27. int main(int argc, char*argv[]) {
  28. int fps = 305; /* fps * 10 */
  29. /* 48000 * 2 * 2 bps, we want chunks corresponding to 30 fps */
  30. const uint32_t bufsize = ((48000 * 2 * 2 * 10) / fps) & ~3;
  31. if (argc != 2) {
  32. printf("usage: prog fps*10, run in recording directory\n");
  33. exit(1);
  34. }
  35. fps = atoi(argv[1]);
  36. /* The sample type to use */
  37. static const pa_sample_spec ss = {
  38. .format = PA_SAMPLE_S16LE,
  39. .rate = 48000,
  40. .channels = 2
  41. };
  42. static pa_buffer_attr attr = {
  43. .minreq = (uint32_t) -1,
  44. .prebuf = (uint32_t) -1,
  45. .tlength = (uint32_t) -1,
  46. };
  47. pa_simple *r = NULL;
  48. int ret = 1;
  49. int error;
  50. const pa_buffer_attr *p_attr = &attr;
  51. int opt = 0; // | PA_STREAM_ADJUST_LATENCY;
  52. uint8_t *buf = malloc(bufsize);
  53. attr.fragsize = bufsize;
  54. attr.maxlength = bufsize;
  55. /* Create the recording stream */
  56. if (!(r = pa_simple_new(NULL, argv[0], PA_STREAM_RECORD | opt, NULL, "record", &ss, NULL, p_attr, &error))) {
  57. fprintf(stderr, __FILE__": pa_simple_new() failed: %s\n", pa_strerror(error));
  58. goto finish;
  59. }
  60. for (;;) {
  61. char name[1024];
  62. int fd, res;
  63. /* Record some data ... */
  64. if (pa_simple_read(r, buf, bufsize, &error) < 0) {
  65. fprintf(stderr, __FILE__": pa_simple_read() failed: %s\n", pa_strerror(error));
  66. goto finish;
  67. }
  68. get_name(name, ".", "48000-s16le-stereo.sa");
  69. fd = open(name, O_WRONLY | O_CREAT | O_EXCL, 0666);
  70. res = write(fd, buf, bufsize);
  71. if (res != bufsize) {
  72. fprintf(stderr, __FILE__": could not write samples: %m\n");
  73. goto finish;
  74. }
  75. close(fd);
  76. }
  77. finish:
  78. if (r)
  79. pa_simple_free(r);
  80. return ret;
  81. }