voice.c 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152
  1. /*
  2. * Copyright (c) by Jaroslav Kysela <perex@suse.cz>
  3. * Creative Labs, Inc.
  4. * Lee Revell <rlrevell@joe-job.com>
  5. * Routines for control of EMU10K1 chips - voice manager
  6. *
  7. * Rewrote voice allocator for multichannel support - rlrevell 12/2004
  8. *
  9. * BUGS:
  10. * --
  11. *
  12. * TODO:
  13. * --
  14. *
  15. * This program is free software; you can redistribute it and/or modify
  16. * it under the terms of the GNU General Public License as published by
  17. * the Free Software Foundation; either version 2 of the License, or
  18. * (at your option) any later version.
  19. *
  20. * This program is distributed in the hope that it will be useful,
  21. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  22. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  23. * GNU General Public License for more details.
  24. *
  25. * You should have received a copy of the GNU General Public License
  26. * along with this program; if not, write to the Free Software
  27. * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  28. *
  29. */
  30. #include <sound/driver.h>
  31. #include <linux/time.h>
  32. #include <sound/core.h>
  33. #include <sound/emu10k1.h>
  34. /* Previously the voice allocator started at 0 every time. The new voice
  35. * allocator uses a round robin scheme. The next free voice is tracked in
  36. * the card record and each allocation begins where the last left off. The
  37. * hardware requires stereo interleaved voices be aligned to an even/odd
  38. * boundary. For multichannel voice allocation we ensure than the block of
  39. * voices does not cross the 32 voice boundary. This simplifies the
  40. * multichannel support and ensures we can use a single write to the
  41. * (set|clear)_loop_stop registers. Otherwise (for example) the voices would
  42. * get out of sync when pausing/resuming a stream.
  43. * --rlrevell
  44. */
  45. static int voice_alloc(emu10k1_t *emu, emu10k1_voice_type_t type, int number, emu10k1_voice_t **rvoice)
  46. {
  47. emu10k1_voice_t *voice;
  48. int i, j, k, first_voice, last_voice, skip;
  49. *rvoice = NULL;
  50. first_voice = last_voice = 0;
  51. for (i = emu->next_free_voice, j = 0; j < NUM_G ; i += number, j += number) {
  52. // printk("i %d j %d next free %d!\n", i, j, emu->next_free_voice);
  53. i %= NUM_G;
  54. /* stereo voices must be even/odd */
  55. if ((number == 2) && (i % 2)) {
  56. i++;
  57. continue;
  58. }
  59. skip = 0;
  60. for (k = 0; k < number; k++) {
  61. voice = &emu->voices[(i+k) % NUM_G];
  62. if (voice->use) {
  63. skip = 1;
  64. break;
  65. }
  66. }
  67. if (!skip) {
  68. // printk("allocated voice %d\n", i);
  69. first_voice = i;
  70. last_voice = (i + number) % NUM_G;
  71. emu->next_free_voice = last_voice;
  72. break;
  73. }
  74. }
  75. if (first_voice == last_voice)
  76. return -ENOMEM;
  77. for (i=0; i < number; i++) {
  78. voice = &emu->voices[(first_voice + i) % NUM_G];
  79. // printk("voice alloc - %i, %i of %i\n", voice->number, idx-first_voice+1, number);
  80. voice->use = 1;
  81. switch (type) {
  82. case EMU10K1_PCM:
  83. voice->pcm = 1;
  84. break;
  85. case EMU10K1_SYNTH:
  86. voice->synth = 1;
  87. break;
  88. case EMU10K1_MIDI:
  89. voice->midi = 1;
  90. break;
  91. case EMU10K1_EFX:
  92. voice->efx = 1;
  93. break;
  94. }
  95. }
  96. *rvoice = &emu->voices[first_voice];
  97. return 0;
  98. }
  99. int snd_emu10k1_voice_alloc(emu10k1_t *emu, emu10k1_voice_type_t type, int number, emu10k1_voice_t **rvoice)
  100. {
  101. unsigned long flags;
  102. int result;
  103. snd_assert(rvoice != NULL, return -EINVAL);
  104. snd_assert(number, return -EINVAL);
  105. spin_lock_irqsave(&emu->voice_lock, flags);
  106. for (;;) {
  107. result = voice_alloc(emu, type, number, rvoice);
  108. if (result == 0 || type == EMU10K1_SYNTH || type == EMU10K1_MIDI)
  109. break;
  110. /* free a voice from synth */
  111. if (emu->get_synth_voice) {
  112. result = emu->get_synth_voice(emu);
  113. if (result >= 0) {
  114. emu10k1_voice_t *pvoice = &emu->voices[result];
  115. pvoice->interrupt = NULL;
  116. pvoice->use = pvoice->pcm = pvoice->synth = pvoice->midi = pvoice->efx = 0;
  117. pvoice->epcm = NULL;
  118. }
  119. }
  120. if (result < 0)
  121. break;
  122. }
  123. spin_unlock_irqrestore(&emu->voice_lock, flags);
  124. return result;
  125. }
  126. int snd_emu10k1_voice_free(emu10k1_t *emu, emu10k1_voice_t *pvoice)
  127. {
  128. unsigned long flags;
  129. snd_assert(pvoice != NULL, return -EINVAL);
  130. spin_lock_irqsave(&emu->voice_lock, flags);
  131. pvoice->interrupt = NULL;
  132. pvoice->use = pvoice->pcm = pvoice->synth = pvoice->midi = pvoice->efx = 0;
  133. pvoice->epcm = NULL;
  134. snd_emu10k1_voice_init(emu, pvoice->number);
  135. spin_unlock_irqrestore(&emu->voice_lock, flags);
  136. return 0;
  137. }