wavcapture.c 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191
  1. #include "qemu/osdep.h"
  2. #include "qemu/qemu-print.h"
  3. #include "qapi/error.h"
  4. #include "qemu/error-report.h"
  5. #include "audio.h"
  6. typedef struct {
  7. FILE *f;
  8. int bytes;
  9. char *path;
  10. int freq;
  11. int bits;
  12. int nchannels;
  13. CaptureVoiceOut *cap;
  14. } WAVState;
  15. /* VICE code: Store number as little endian. */
  16. static void le_store (uint8_t *buf, uint32_t val, int len)
  17. {
  18. int i;
  19. for (i = 0; i < len; i++) {
  20. buf[i] = (uint8_t) (val & 0xff);
  21. val >>= 8;
  22. }
  23. }
  24. static void wav_notify (void *opaque, audcnotification_e cmd)
  25. {
  26. (void) opaque;
  27. (void) cmd;
  28. }
  29. static void wav_destroy (void *opaque)
  30. {
  31. WAVState *wav = opaque;
  32. uint8_t rlen[4];
  33. uint8_t dlen[4];
  34. uint32_t datalen = wav->bytes;
  35. uint32_t rifflen = datalen + 36;
  36. if (wav->f) {
  37. le_store (rlen, rifflen, 4);
  38. le_store (dlen, datalen, 4);
  39. if (fseek (wav->f, 4, SEEK_SET)) {
  40. error_report("wav_destroy: rlen fseek failed: %s",
  41. strerror(errno));
  42. goto doclose;
  43. }
  44. if (fwrite (rlen, 4, 1, wav->f) != 1) {
  45. error_report("wav_destroy: rlen fwrite failed: %s",
  46. strerror(errno));
  47. goto doclose;
  48. }
  49. if (fseek (wav->f, 32, SEEK_CUR)) {
  50. error_report("wav_destroy: dlen fseek failed: %s",
  51. strerror(errno));
  52. goto doclose;
  53. }
  54. if (fwrite (dlen, 1, 4, wav->f) != 4) {
  55. error_report("wav_destroy: dlen fwrite failed: %s",
  56. strerror(errno));
  57. goto doclose;
  58. }
  59. doclose:
  60. if (fclose (wav->f)) {
  61. error_report("wav_destroy: fclose failed: %s", strerror(errno));
  62. }
  63. }
  64. g_free (wav->path);
  65. }
  66. static void wav_capture(void *opaque, const void *buf, int size)
  67. {
  68. WAVState *wav = opaque;
  69. if (fwrite (buf, size, 1, wav->f) != 1) {
  70. error_report("wav_capture: fwrite error: %s", strerror(errno));
  71. }
  72. wav->bytes += size;
  73. }
  74. static void wav_capture_destroy (void *opaque)
  75. {
  76. WAVState *wav = opaque;
  77. AUD_del_capture (wav->cap, wav);
  78. g_free (wav);
  79. }
  80. static void wav_capture_info (void *opaque)
  81. {
  82. WAVState *wav = opaque;
  83. char *path = wav->path;
  84. qemu_printf("Capturing audio(%d,%d,%d) to %s: %d bytes\n",
  85. wav->freq, wav->bits, wav->nchannels,
  86. path ? path : "<not available>", wav->bytes);
  87. }
  88. static struct capture_ops wav_capture_ops = {
  89. .destroy = wav_capture_destroy,
  90. .info = wav_capture_info
  91. };
  92. int wav_start_capture(AudioState *state, CaptureState *s, const char *path,
  93. int freq, int bits, int nchannels)
  94. {
  95. WAVState *wav;
  96. uint8_t hdr[] = {
  97. 0x52, 0x49, 0x46, 0x46, 0x00, 0x00, 0x00, 0x00, 0x57, 0x41, 0x56,
  98. 0x45, 0x66, 0x6d, 0x74, 0x20, 0x10, 0x00, 0x00, 0x00, 0x01, 0x00,
  99. 0x02, 0x00, 0x44, 0xac, 0x00, 0x00, 0x10, 0xb1, 0x02, 0x00, 0x04,
  100. 0x00, 0x10, 0x00, 0x64, 0x61, 0x74, 0x61, 0x00, 0x00, 0x00, 0x00
  101. };
  102. struct audsettings as;
  103. struct audio_capture_ops ops;
  104. int stereo, bits16, shift;
  105. CaptureVoiceOut *cap;
  106. if (bits != 8 && bits != 16) {
  107. error_report("incorrect bit count %d, must be 8 or 16", bits);
  108. return -1;
  109. }
  110. if (nchannels != 1 && nchannels != 2) {
  111. error_report("incorrect channel count %d, must be 1 or 2",
  112. nchannels);
  113. return -1;
  114. }
  115. stereo = nchannels == 2;
  116. bits16 = bits == 16;
  117. as.freq = freq;
  118. as.nchannels = 1 << stereo;
  119. as.fmt = bits16 ? AUDIO_FORMAT_S16 : AUDIO_FORMAT_U8;
  120. as.endianness = 0;
  121. ops.notify = wav_notify;
  122. ops.capture = wav_capture;
  123. ops.destroy = wav_destroy;
  124. wav = g_malloc0 (sizeof (*wav));
  125. shift = bits16 + stereo;
  126. hdr[34] = bits16 ? 0x10 : 0x08;
  127. le_store (hdr + 22, as.nchannels, 2);
  128. le_store (hdr + 24, freq, 4);
  129. le_store (hdr + 28, freq << shift, 4);
  130. le_store (hdr + 32, 1 << shift, 2);
  131. wav->f = fopen (path, "wb");
  132. if (!wav->f) {
  133. error_report("Failed to open wave file `%s': %s",
  134. path, strerror(errno));
  135. g_free (wav);
  136. return -1;
  137. }
  138. wav->path = g_strdup (path);
  139. wav->bits = bits;
  140. wav->nchannels = nchannels;
  141. wav->freq = freq;
  142. if (fwrite (hdr, sizeof (hdr), 1, wav->f) != 1) {
  143. error_report("Failed to write header: %s", strerror(errno));
  144. goto error_free;
  145. }
  146. cap = AUD_add_capture(state, &as, &ops, wav);
  147. if (!cap) {
  148. error_report("Failed to add audio capture");
  149. goto error_free;
  150. }
  151. wav->cap = cap;
  152. s->opaque = wav;
  153. s->ops = wav_capture_ops;
  154. return 0;
  155. error_free:
  156. g_free (wav->path);
  157. if (fclose (wav->f)) {
  158. error_report("Failed to close wave file: %s", strerror(errno));
  159. }
  160. g_free (wav);
  161. return -1;
  162. }