voice.c 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155
  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(struct snd_emu10k1 *emu, int type, int number,
  46. struct snd_emu10k1_voice **rvoice)
  47. {
  48. struct snd_emu10k1_voice *voice;
  49. int i, j, k, first_voice, last_voice, skip;
  50. *rvoice = NULL;
  51. first_voice = last_voice = 0;
  52. for (i = emu->next_free_voice, j = 0; j < NUM_G ; i += number, j += number) {
  53. // printk("i %d j %d next free %d!\n", i, j, emu->next_free_voice);
  54. i %= NUM_G;
  55. /* stereo voices must be even/odd */
  56. if ((number == 2) && (i % 2)) {
  57. i++;
  58. continue;
  59. }
  60. skip = 0;
  61. for (k = 0; k < number; k++) {
  62. voice = &emu->voices[(i+k) % NUM_G];
  63. if (voice->use) {
  64. skip = 1;
  65. break;
  66. }
  67. }
  68. if (!skip) {
  69. // printk("allocated voice %d\n", i);
  70. first_voice = i;
  71. last_voice = (i + number) % NUM_G;
  72. emu->next_free_voice = last_voice;
  73. break;
  74. }
  75. }
  76. if (first_voice == last_voice)
  77. return -ENOMEM;
  78. for (i=0; i < number; i++) {
  79. voice = &emu->voices[(first_voice + i) % NUM_G];
  80. // printk("voice alloc - %i, %i of %i\n", voice->number, idx-first_voice+1, number);
  81. voice->use = 1;
  82. switch (type) {
  83. case EMU10K1_PCM:
  84. voice->pcm = 1;
  85. break;
  86. case EMU10K1_SYNTH:
  87. voice->synth = 1;
  88. break;
  89. case EMU10K1_MIDI:
  90. voice->midi = 1;
  91. break;
  92. case EMU10K1_EFX:
  93. voice->efx = 1;
  94. break;
  95. }
  96. }
  97. *rvoice = &emu->voices[first_voice];
  98. return 0;
  99. }
  100. int snd_emu10k1_voice_alloc(struct snd_emu10k1 *emu, int type, int number,
  101. struct snd_emu10k1_voice **rvoice)
  102. {
  103. unsigned long flags;
  104. int result;
  105. snd_assert(rvoice != NULL, return -EINVAL);
  106. snd_assert(number, return -EINVAL);
  107. spin_lock_irqsave(&emu->voice_lock, flags);
  108. for (;;) {
  109. result = voice_alloc(emu, type, number, rvoice);
  110. if (result == 0 || type == EMU10K1_SYNTH || type == EMU10K1_MIDI)
  111. break;
  112. /* free a voice from synth */
  113. if (emu->get_synth_voice) {
  114. result = emu->get_synth_voice(emu);
  115. if (result >= 0) {
  116. struct snd_emu10k1_voice *pvoice = &emu->voices[result];
  117. pvoice->interrupt = NULL;
  118. pvoice->use = pvoice->pcm = pvoice->synth = pvoice->midi = pvoice->efx = 0;
  119. pvoice->epcm = NULL;
  120. }
  121. }
  122. if (result < 0)
  123. break;
  124. }
  125. spin_unlock_irqrestore(&emu->voice_lock, flags);
  126. return result;
  127. }
  128. int snd_emu10k1_voice_free(struct snd_emu10k1 *emu,
  129. struct snd_emu10k1_voice *pvoice)
  130. {
  131. unsigned long flags;
  132. snd_assert(pvoice != NULL, return -EINVAL);
  133. spin_lock_irqsave(&emu->voice_lock, flags);
  134. pvoice->interrupt = NULL;
  135. pvoice->use = pvoice->pcm = pvoice->synth = pvoice->midi = pvoice->efx = 0;
  136. pvoice->epcm = NULL;
  137. snd_emu10k1_voice_init(emu, pvoice->number);
  138. spin_unlock_irqrestore(&emu->voice_lock, flags);
  139. return 0;
  140. }