PageRenderTime 60ms CodeModel.GetById 20ms RepoModel.GetById 0ms app.codeStats 1ms

/tinycap.c

https://bitbucket.org/cyanogenmod/android_external_tinyalsa
C | 217 lines | 161 code | 25 blank | 31 comment | 43 complexity | c712ae0a551e322657d08eeb029dfc16 MD5 | raw file
  1. /* tinycap.c
  2. **
  3. ** Copyright 2011, The Android Open Source Project
  4. **
  5. ** Redistribution and use in source and binary forms, with or without
  6. ** modification, are permitted provided that the following conditions are met:
  7. ** * Redistributions of source code must retain the above copyright
  8. ** notice, this list of conditions and the following disclaimer.
  9. ** * Redistributions in binary form must reproduce the above copyright
  10. ** notice, this list of conditions and the following disclaimer in the
  11. ** documentation and/or other materials provided with the distribution.
  12. ** * Neither the name of The Android Open Source Project nor the names of
  13. ** its contributors may be used to endorse or promote products derived
  14. ** from this software without specific prior written permission.
  15. **
  16. ** THIS SOFTWARE IS PROVIDED BY The Android Open Source Project ``AS IS'' AND
  17. ** ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  18. ** IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  19. ** ARE DISCLAIMED. IN NO EVENT SHALL The Android Open Source Project BE LIABLE
  20. ** FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  21. ** DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
  22. ** SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
  23. ** CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  24. ** LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
  25. ** OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH
  26. ** DAMAGE.
  27. */
  28. #include <tinyalsa/asoundlib.h>
  29. #include <stdio.h>
  30. #include <stdlib.h>
  31. #include <stdint.h>
  32. #include <signal.h>
  33. #define ID_RIFF 0x46464952
  34. #define ID_WAVE 0x45564157
  35. #define ID_FMT 0x20746d66
  36. #define ID_DATA 0x61746164
  37. #define FORMAT_PCM 1
  38. struct wav_header {
  39. uint32_t riff_id;
  40. uint32_t riff_sz;
  41. uint32_t riff_fmt;
  42. uint32_t fmt_id;
  43. uint32_t fmt_sz;
  44. uint16_t audio_format;
  45. uint16_t num_channels;
  46. uint32_t sample_rate;
  47. uint32_t byte_rate;
  48. uint16_t block_align;
  49. uint16_t bits_per_sample;
  50. uint32_t data_id;
  51. uint32_t data_sz;
  52. };
  53. int capturing = 1;
  54. unsigned int capture_sample(FILE *file, unsigned int card, unsigned int device,
  55. unsigned int channels, unsigned int rate,
  56. unsigned int bits, unsigned int period_size,
  57. unsigned int period_count);
  58. void sigint_handler(int sig)
  59. {
  60. capturing = 0;
  61. }
  62. int main(int argc, char **argv)
  63. {
  64. FILE *file;
  65. struct wav_header header;
  66. unsigned int card = 0;
  67. unsigned int device = 0;
  68. unsigned int channels = 2;
  69. unsigned int rate = 44100;
  70. unsigned int bits = 16;
  71. unsigned int frames;
  72. unsigned int period_size = 1024;
  73. unsigned int period_count = 4;
  74. if (argc < 2) {
  75. fprintf(stderr, "Usage: %s file.wav [-D card] [-d device] [-c channels] "
  76. "[-r rate] [-b bits] [-p period_size] [-n n_periods]\n", argv[0]);
  77. return 1;
  78. }
  79. file = fopen(argv[1], "wb");
  80. if (!file) {
  81. fprintf(stderr, "Unable to create file '%s'\n", argv[1]);
  82. return 1;
  83. }
  84. /* parse command line arguments */
  85. argv += 2;
  86. while (*argv) {
  87. if (strcmp(*argv, "-d") == 0) {
  88. argv++;
  89. if (*argv)
  90. device = atoi(*argv);
  91. } else if (strcmp(*argv, "-c") == 0) {
  92. argv++;
  93. if (*argv)
  94. channels = atoi(*argv);
  95. } else if (strcmp(*argv, "-r") == 0) {
  96. argv++;
  97. if (*argv)
  98. rate = atoi(*argv);
  99. } else if (strcmp(*argv, "-b") == 0) {
  100. argv++;
  101. if (*argv)
  102. bits = atoi(*argv);
  103. } else if (strcmp(*argv, "-D") == 0) {
  104. argv++;
  105. if (*argv)
  106. card = atoi(*argv);
  107. } else if (strcmp(*argv, "-p") == 0) {
  108. argv++;
  109. if (*argv)
  110. period_size = atoi(*argv);
  111. } else if (strcmp(*argv, "-n") == 0) {
  112. argv++;
  113. if (*argv)
  114. period_count = atoi(*argv);
  115. }
  116. if (*argv)
  117. argv++;
  118. }
  119. header.riff_id = ID_RIFF;
  120. header.riff_sz = 0;
  121. header.riff_fmt = ID_WAVE;
  122. header.fmt_id = ID_FMT;
  123. header.fmt_sz = 16;
  124. header.audio_format = FORMAT_PCM;
  125. header.num_channels = channels;
  126. header.sample_rate = rate;
  127. header.byte_rate = (header.bits_per_sample / 8) * channels * rate;
  128. header.block_align = channels * (header.bits_per_sample / 8);
  129. header.bits_per_sample = bits;
  130. header.data_id = ID_DATA;
  131. /* leave enough room for header */
  132. fseek(file, sizeof(struct wav_header), SEEK_SET);
  133. /* install signal handler and begin capturing */
  134. signal(SIGINT, sigint_handler);
  135. frames = capture_sample(file, card, device, header.num_channels,
  136. header.sample_rate, header.bits_per_sample,
  137. period_size, period_count);
  138. printf("Captured %d frames\n", frames);
  139. /* write header now all information is known */
  140. header.data_sz = frames * header.block_align;
  141. fseek(file, 0, SEEK_SET);
  142. fwrite(&header, sizeof(struct wav_header), 1, file);
  143. fclose(file);
  144. return 0;
  145. }
  146. unsigned int capture_sample(FILE *file, unsigned int card, unsigned int device,
  147. unsigned int channels, unsigned int rate,
  148. unsigned int bits, unsigned int period_size,
  149. unsigned int period_count)
  150. {
  151. struct pcm_config config;
  152. struct pcm *pcm;
  153. char *buffer;
  154. unsigned int size;
  155. unsigned int bytes_read = 0;
  156. config.channels = channels;
  157. config.rate = rate;
  158. config.period_size = period_size;
  159. config.period_count = period_count;
  160. if (bits == 32)
  161. config.format = PCM_FORMAT_S32_LE;
  162. else if (bits == 16)
  163. config.format = PCM_FORMAT_S16_LE;
  164. config.start_threshold = 0;
  165. config.stop_threshold = 0;
  166. config.silence_threshold = 0;
  167. pcm = pcm_open(card, device, PCM_IN, &config);
  168. if (!pcm || !pcm_is_ready(pcm)) {
  169. fprintf(stderr, "Unable to open PCM device (%s)\n",
  170. pcm_get_error(pcm));
  171. return 0;
  172. }
  173. size = pcm_get_buffer_size(pcm);
  174. buffer = malloc(size);
  175. if (!buffer) {
  176. fprintf(stderr, "Unable to allocate %d bytes\n", size);
  177. free(buffer);
  178. pcm_close(pcm);
  179. return 0;
  180. }
  181. printf("Capturing sample: %u ch, %u hz, %u bit\n", channels, rate, bits);
  182. while (capturing && !pcm_read(pcm, buffer, size)) {
  183. if (fwrite(buffer, 1, size, file) != size) {
  184. fprintf(stderr,"Error capturing sample\n");
  185. break;
  186. }
  187. bytes_read += size;
  188. }
  189. free(buffer);
  190. pcm_close(pcm);
  191. return bytes_read / ((bits / 8) * channels);
  192. }