snd.c 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324
  1. /* vim: tabstop=4 shiftwidth=4 noexpandtab
  2. * This file is part of ToaruOS and is released under the terms
  3. * of the NCSA / University of Illinois License - see LICENSE.md
  4. * Copyright (C) 2015 Mike Gerow
  5. *
  6. * Sound subsystem.
  7. *
  8. * Currently has the ability to mix several sound sources together. Could use
  9. * a /dev/mixer device to allow changing of audio settings. Also could use
  10. * the ability to change frequency and format for audio samples. Also doesn't
  11. * really support multiple devices despite the interface suggesting it might...
  12. */
  13. #include <kernel/module.h>
  14. #include <kernel/ringbuffer.h>
  15. #include <kernel/system.h>
  16. #include <kernel/mod/snd.h>
  17. #include <toaru/list.h>
  18. #include <errno.h>
  19. /* Utility macros */
  20. #define N_ELEMENTS(arr) (sizeof(arr) / sizeof((arr)[0]))
  21. #define SND_BUF_SIZE 0x4000
  22. static uint32_t snd_dsp_write(fs_node_t * node, uint64_t offset, uint32_t size, uint8_t *buffer);
  23. static int snd_dsp_ioctl(fs_node_t * node, int request, void * argp);
  24. static void snd_dsp_open(fs_node_t * node, unsigned int flags);
  25. static void snd_dsp_close(fs_node_t * node);
  26. static int snd_mixer_ioctl(fs_node_t * node, int request, void * argp);
  27. static void snd_mixer_open(fs_node_t * node, unsigned int flags);
  28. static void snd_mixer_close(fs_node_t * node);
  29. static spin_lock_t _devices_lock;
  30. static list_t _devices;
  31. static fs_node_t _dsp_fnode = {
  32. .name = "dsp",
  33. .device = &_devices,
  34. .mask = 0666,
  35. .flags = FS_CHARDEVICE,
  36. .ioctl = snd_dsp_ioctl,
  37. .write = snd_dsp_write,
  38. .open = snd_dsp_open,
  39. .close = snd_dsp_close,
  40. };
  41. static fs_node_t _mixer_fnode = {
  42. .name = "mixer",
  43. .mask = 0666,
  44. .flags = FS_CHARDEVICE,
  45. .ioctl = snd_mixer_ioctl,
  46. .open = snd_mixer_open,
  47. .close = snd_mixer_close,
  48. };
  49. static spin_lock_t _buffers_lock;
  50. static list_t _buffers;
  51. static uint32_t _next_device_id = SND_DEVICE_MAIN;
  52. struct dsp_node {
  53. ring_buffer_t * rb;
  54. size_t samples;
  55. size_t written;
  56. int realtime;
  57. };
  58. int snd_register(snd_device_t * device) {
  59. int rv = 0;
  60. debug_print(WARNING, "[snd] _devices lock: %d", _devices_lock);
  61. spin_lock(_devices_lock);
  62. device->id = _next_device_id;
  63. _next_device_id++;
  64. if (list_find(&_devices, device)) {
  65. debug_print(WARNING, "[snd] attempt to register duplicate %s", device->name);
  66. rv = -1;
  67. goto snd_register_cleanup;
  68. }
  69. list_insert(&_devices, device);
  70. debug_print(NOTICE, "[snd] %s registered", device->name);
  71. snd_register_cleanup:
  72. spin_unlock(_devices_lock);
  73. return rv;
  74. }
  75. int snd_unregister(snd_device_t * device) {
  76. int rv = 0;
  77. node_t * node = list_find(&_devices, device);
  78. if (!node) {
  79. debug_print(WARNING, "[snd] attempted to unregister %s, "
  80. "but it was never registered", device->name);
  81. goto snd_unregister_cleanup;
  82. }
  83. list_delete(&_devices, node);
  84. debug_print(NOTICE, "[snd] %s unregistered", device->name);
  85. snd_unregister_cleanup:
  86. spin_unlock(_devices_lock);
  87. return rv;
  88. }
  89. static uint32_t snd_dsp_write(fs_node_t * node, uint64_t offset, uint32_t size, uint8_t *buffer) {
  90. if (!_devices.length) return -1; /* No sink available. */
  91. struct dsp_node * dsp = node->device;
  92. size_t s = ring_buffer_available(dsp->rb);
  93. size_t out;
  94. if (size > s && dsp->realtime) {
  95. out = ring_buffer_write(dsp->rb, s & ~0x3, buffer);
  96. } else {
  97. out = ring_buffer_write(dsp->rb, size, buffer);
  98. }
  99. dsp->written += out / 4;
  100. return out;
  101. }
  102. static int snd_dsp_ioctl(fs_node_t * node, int request, void * argp) {
  103. /* Potentially use this to set sample rates in the future */
  104. struct dsp_node * dsp = node->device;
  105. if (request == 4) {
  106. dsp->realtime = 1;
  107. } else if (request == 5) {
  108. return dsp->samples;
  109. }
  110. return -1;
  111. }
  112. static void snd_dsp_open(fs_node_t * node, unsigned int flags) {
  113. /*
  114. * XXX(gerow): A process could take the memory of the entire system by opening
  115. * too many of these...
  116. */
  117. /* Allocate a buffer for the node and keep a reference for ourselves */
  118. struct dsp_node * dsp = malloc(sizeof(struct dsp_node));
  119. dsp->rb = ring_buffer_create(SND_BUF_SIZE);
  120. dsp->samples = 0;
  121. dsp->written = 0;
  122. dsp->realtime = 0;
  123. node->device = dsp;
  124. spin_lock(_buffers_lock);
  125. list_insert(&_buffers, node->device);
  126. spin_unlock(_buffers_lock);
  127. }
  128. static void snd_dsp_close(fs_node_t * node) {
  129. struct dsp_node * dsp = node->device;
  130. spin_lock(_buffers_lock);
  131. list_delete(&_buffers, list_find(&_buffers, dsp));
  132. spin_unlock(_buffers_lock);
  133. ring_buffer_destroy(dsp->rb);
  134. free(dsp->rb);
  135. free(dsp);
  136. }
  137. static snd_device_t * snd_device_by_id(uint32_t device_id) {
  138. spin_lock(_devices_lock);
  139. snd_device_t * out = NULL;
  140. snd_device_t * cur = NULL;
  141. foreach(node, &_devices) {
  142. cur = node->value;
  143. if (cur->id == device_id) {
  144. out = cur;
  145. }
  146. }
  147. spin_unlock(_devices_lock);
  148. return out;
  149. }
  150. static int snd_mixer_ioctl(fs_node_t * node, int request, void * argp) {
  151. switch (request) {
  152. case SND_MIXER_GET_KNOBS: {
  153. snd_knob_list_t * list = argp;
  154. snd_device_t * device = snd_device_by_id(list->device);
  155. if (!device) {
  156. return -EINVAL;
  157. }
  158. list->num = device->num_knobs;
  159. for (uint32_t i = 0; i < device->num_knobs; i++) {
  160. list->ids[i] = device->knobs[i].id;
  161. }
  162. return 0;
  163. }
  164. case SND_MIXER_GET_KNOB_INFO: {
  165. snd_knob_info_t * info = argp;
  166. snd_device_t * device = snd_device_by_id(info->device);
  167. if (!device) {
  168. return -EINVAL;
  169. }
  170. for (uint32_t i = 0; i < device->num_knobs; i++) {
  171. if (device->knobs[i].id == info->id) {
  172. memcpy(info->name, device->knobs[i].name, sizeof(info->name));
  173. return 0;
  174. }
  175. }
  176. return -EINVAL;
  177. }
  178. case SND_MIXER_READ_KNOB: {
  179. snd_knob_value_t * value = argp;
  180. snd_device_t * device = snd_device_by_id(value->device);
  181. if (!device) {
  182. return -EINVAL;
  183. }
  184. return device->mixer_read(value->id, &value->val);
  185. }
  186. case SND_MIXER_WRITE_KNOB: {
  187. snd_knob_value_t * value = argp;
  188. snd_device_t * device = snd_device_by_id(value->device);
  189. if (!device) {
  190. return -EINVAL;
  191. }
  192. return device->mixer_write(value->id, value->val);
  193. }
  194. default: {
  195. return -EINVAL;
  196. }
  197. }
  198. }
  199. static void snd_mixer_open(fs_node_t * node, unsigned int flags) {
  200. return;
  201. }
  202. static void snd_mixer_close(fs_node_t * node) {
  203. return;
  204. }
  205. int snd_request_buf(snd_device_t * device, uint32_t size, uint8_t *buffer) {
  206. static int16_t tmp_buf[0x100];
  207. memset(buffer, 0, size);
  208. spin_lock(_buffers_lock);
  209. foreach(buf_node, &_buffers) {
  210. struct dsp_node * dsp = buf_node->value;
  211. ring_buffer_t * buf = dsp->rb;
  212. /* ~0x3 is to ensure we don't read partial samples or just a single channel */
  213. size_t bytes_left = MIN(ring_buffer_unread(buf) & ~0x3, size);
  214. int16_t * adding_ptr = (int16_t *) buffer;
  215. while (bytes_left) {
  216. size_t this_read_size = MIN(bytes_left, sizeof(tmp_buf));
  217. ring_buffer_read(buf, this_read_size, (uint8_t *)tmp_buf);
  218. dsp->samples += this_read_size / 4; /* 16 bits, 2 channels */
  219. /*
  220. * Reduce the sample by a half so that multiple sources won't immediately
  221. * cause awful clipping. This is kind of a hack since it would probably be
  222. * better to just use some kind of compressor.
  223. */
  224. for (size_t i = 0; i < N_ELEMENTS(tmp_buf); i++) {
  225. tmp_buf[i] /= 2;
  226. }
  227. for (size_t i = 0; i < this_read_size / sizeof(*adding_ptr); i++) {
  228. adding_ptr[i] += tmp_buf[i];
  229. }
  230. adding_ptr += this_read_size / sizeof(*adding_ptr);
  231. bytes_left -= this_read_size;
  232. }
  233. }
  234. spin_unlock(_buffers_lock);
  235. return size;
  236. }
  237. static snd_device_t * snd_main_device() {
  238. spin_lock(_devices_lock);
  239. foreach(node, &_devices) {
  240. spin_unlock(_devices_lock);
  241. return node->value;
  242. }
  243. spin_unlock(_devices_lock);
  244. return NULL;
  245. }
  246. #if 0
  247. #include <mod/shell.h>
  248. DEFINE_SHELL_FUNCTION(snd_full, "[debug] turn snd master to full") {
  249. snd_main_device()->mixer_write(SND_KNOB_MASTER, UINT32_MAX);
  250. return 0;
  251. }
  252. DEFINE_SHELL_FUNCTION(snd_half, "[debug] turn snd master to half") {
  253. snd_main_device()->mixer_write(SND_KNOB_MASTER, UINT32_MAX / 2);
  254. return 0;
  255. }
  256. DEFINE_SHELL_FUNCTION(snd_off, "[debug] turn snd master to lowest volume") {
  257. snd_main_device()->mixer_write(SND_KNOB_MASTER, 0);
  258. return 0;
  259. }
  260. #endif
  261. static int init(void) {
  262. vfs_mount("/dev/dsp", &_dsp_fnode);
  263. vfs_mount("/dev/mixer", &_mixer_fnode);
  264. #if 0
  265. BIND_SHELL_FUNCTION(snd_full);
  266. BIND_SHELL_FUNCTION(snd_half);
  267. BIND_SHELL_FUNCTION(snd_off);
  268. #endif
  269. return 0;
  270. }
  271. static int fini(void) {
  272. /* umount? */
  273. return 0;
  274. }
  275. MODULE_DEF(snd, init, fini);
  276. #if 0
  277. MODULE_DEPENDS(debugshell);
  278. #endif