voice.c 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161
  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. // 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(struct snd_emu10k1 *emu, int type, int number,
  100. struct snd_emu10k1_voice **rvoice)
  101. {
  102. unsigned long flags;
  103. int result;
  104. if (snd_BUG_ON(!rvoice))
  105. return -EINVAL;
  106. if (snd_BUG_ON(!number))
  107. return -EINVAL;
  108. spin_lock_irqsave(&emu->voice_lock, flags);
  109. for (;;) {
  110. result = voice_alloc(emu, type, number, rvoice);
  111. if (result == 0 || type == EMU10K1_SYNTH || type == EMU10K1_MIDI)
  112. break;
  113. /* free a voice from synth */
  114. if (emu->get_synth_voice) {
  115. result = emu->get_synth_voice(emu);
  116. if (result >= 0) {
  117. struct snd_emu10k1_voice *pvoice = &emu->voices[result];
  118. pvoice->interrupt = NULL;
  119. pvoice->use = pvoice->pcm = pvoice->synth = pvoice->midi = pvoice->efx = 0;
  120. pvoice->epcm = NULL;
  121. }
  122. }
  123. if (result < 0)
  124. break;
  125. }
  126. spin_unlock_irqrestore(&emu->voice_lock, flags);
  127. return result;
  128. }
  129. EXPORT_SYMBOL(snd_emu10k1_voice_alloc);
  130. int snd_emu10k1_voice_free(struct snd_emu10k1 *emu,
  131. struct snd_emu10k1_voice *pvoice)
  132. {
  133. unsigned long flags;
  134. if (snd_BUG_ON(!pvoice))
  135. return -EINVAL;
  136. spin_lock_irqsave(&emu->voice_lock, flags);
  137. pvoice->interrupt = NULL;
  138. pvoice->use = pvoice->pcm = pvoice->synth = pvoice->midi = pvoice->efx = 0;
  139. pvoice->epcm = NULL;
  140. snd_emu10k1_voice_init(emu, pvoice->number);
  141. spin_unlock_irqrestore(&emu->voice_lock, flags);
  142. return 0;
  143. }
  144. EXPORT_SYMBOL(snd_emu10k1_voice_free);