voice.c 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168
  1. /*
  2. * Copyright (c) by Jaroslav Kysela <perex@perex.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 <linux/time.h>
  31. #include <sound/core.h>
  32. #include <sound/emu10k1.h>
  33. /* Previously the voice allocator started at 0 every time. The new voice
  34. * allocator uses a round robin scheme. The next free voice is tracked in
  35. * the card record and each allocation begins where the last left off. The
  36. * hardware requires stereo interleaved voices be aligned to an even/odd
  37. * boundary. For multichannel voice allocation we ensure than the block of
  38. * voices does not cross the 32 voice boundary. This simplifies the
  39. * multichannel support and ensures we can use a single write to the
  40. * (set|clear)_loop_stop registers. Otherwise (for example) the voices would
  41. * get out of sync when pausing/resuming a stream.
  42. * --rlrevell
  43. */
  44. static int voice_alloc(struct snd_emu10k1 *emu, int type, int number,
  45. struct snd_emu10k1_voice **rvoice)
  46. {
  47. struct snd_emu10k1_voice *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. /*
  53. printk(KERN_DEBUG "i %d j %d next free %d!\n",
  54. i, j, emu->next_free_voice);
  55. */
  56. i %= NUM_G;
  57. /* stereo voices must be even/odd */
  58. if ((number == 2) && (i % 2)) {
  59. i++;
  60. continue;
  61. }
  62. skip = 0;
  63. for (k = 0; k < number; k++) {
  64. voice = &emu->voices[(i+k) % NUM_G];
  65. if (voice->use) {
  66. skip = 1;
  67. break;
  68. }
  69. }
  70. if (!skip) {
  71. /* printk(KERN_DEBUG "allocated voice %d\n", i); */
  72. first_voice = i;
  73. last_voice = (i + number) % NUM_G;
  74. emu->next_free_voice = last_voice;
  75. break;
  76. }
  77. }
  78. if (first_voice == last_voice)
  79. return -ENOMEM;
  80. for (i = 0; i < number; i++) {
  81. voice = &emu->voices[(first_voice + i) % NUM_G];
  82. /*
  83. printk(kERN_DEBUG "voice alloc - %i, %i of %i\n",
  84. voice->number, idx-first_voice+1, number);
  85. */
  86. voice->use = 1;
  87. switch (type) {
  88. case EMU10K1_PCM:
  89. voice->pcm = 1;
  90. break;
  91. case EMU10K1_SYNTH:
  92. voice->synth = 1;
  93. break;
  94. case EMU10K1_MIDI:
  95. voice->midi = 1;
  96. break;
  97. case EMU10K1_EFX:
  98. voice->efx = 1;
  99. break;
  100. }
  101. }
  102. *rvoice = &emu->voices[first_voice];
  103. return 0;
  104. }
  105. int snd_emu10k1_voice_alloc(struct snd_emu10k1 *emu, int type, int number,
  106. struct snd_emu10k1_voice **rvoice)
  107. {
  108. unsigned long flags;
  109. int result;
  110. if (snd_BUG_ON(!rvoice))
  111. return -EINVAL;
  112. if (snd_BUG_ON(!number))
  113. return -EINVAL;
  114. spin_lock_irqsave(&emu->voice_lock, flags);
  115. for (;;) {
  116. result = voice_alloc(emu, type, number, rvoice);
  117. if (result == 0 || type == EMU10K1_SYNTH || type == EMU10K1_MIDI)
  118. break;
  119. /* free a voice from synth */
  120. if (emu->get_synth_voice) {
  121. result = emu->get_synth_voice(emu);
  122. if (result >= 0) {
  123. struct snd_emu10k1_voice *pvoice = &emu->voices[result];
  124. pvoice->interrupt = NULL;
  125. pvoice->use = pvoice->pcm = pvoice->synth = pvoice->midi = pvoice->efx = 0;
  126. pvoice->epcm = NULL;
  127. }
  128. }
  129. if (result < 0)
  130. break;
  131. }
  132. spin_unlock_irqrestore(&emu->voice_lock, flags);
  133. return result;
  134. }
  135. EXPORT_SYMBOL(snd_emu10k1_voice_alloc);
  136. int snd_emu10k1_voice_free(struct snd_emu10k1 *emu,
  137. struct snd_emu10k1_voice *pvoice)
  138. {
  139. unsigned long flags;
  140. if (snd_BUG_ON(!pvoice))
  141. return -EINVAL;
  142. spin_lock_irqsave(&emu->voice_lock, flags);
  143. pvoice->interrupt = NULL;
  144. pvoice->use = pvoice->pcm = pvoice->synth = pvoice->midi = pvoice->efx = 0;
  145. pvoice->epcm = NULL;
  146. snd_emu10k1_voice_init(emu, pvoice->number);
  147. spin_unlock_irqrestore(&emu->voice_lock, flags);
  148. return 0;
  149. }
  150. EXPORT_SYMBOL(snd_emu10k1_voice_free);