seq_oss_event.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448
  1. /*
  2. * OSS compatible sequencer driver
  3. *
  4. * Copyright (C) 1998,99 Takashi Iwai <tiwai@suse.de>
  5. *
  6. * This program is free software; you can redistribute it and/or modify
  7. * it under the terms of the GNU General Public License as published by
  8. * the Free Software Foundation; either version 2 of the License, or
  9. * (at your option) any later version.
  10. *
  11. * This program is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. * GNU General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU General Public License
  17. * along with this program; if not, write to the Free Software
  18. * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  19. */
  20. #include "seq_oss_device.h"
  21. #include "seq_oss_synth.h"
  22. #include "seq_oss_midi.h"
  23. #include "seq_oss_event.h"
  24. #include "seq_oss_timer.h"
  25. #include <sound/seq_oss_legacy.h>
  26. #include "seq_oss_readq.h"
  27. #include "seq_oss_writeq.h"
  28. /*
  29. * prototypes
  30. */
  31. static int extended_event(struct seq_oss_devinfo *dp, union evrec *q, struct snd_seq_event *ev);
  32. static int chn_voice_event(struct seq_oss_devinfo *dp, union evrec *event_rec, struct snd_seq_event *ev);
  33. static int chn_common_event(struct seq_oss_devinfo *dp, union evrec *event_rec, struct snd_seq_event *ev);
  34. static int timing_event(struct seq_oss_devinfo *dp, union evrec *event_rec, struct snd_seq_event *ev);
  35. static int local_event(struct seq_oss_devinfo *dp, union evrec *event_rec, struct snd_seq_event *ev);
  36. static int old_event(struct seq_oss_devinfo *dp, union evrec *q, struct snd_seq_event *ev);
  37. static int note_on_event(struct seq_oss_devinfo *dp, int dev, int ch, int note, int vel, struct snd_seq_event *ev);
  38. static int note_off_event(struct seq_oss_devinfo *dp, int dev, int ch, int note, int vel, struct snd_seq_event *ev);
  39. static int set_note_event(struct seq_oss_devinfo *dp, int dev, int type, int ch, int note, int vel, struct snd_seq_event *ev);
  40. static int set_control_event(struct seq_oss_devinfo *dp, int dev, int type, int ch, int param, int val, struct snd_seq_event *ev);
  41. static int set_echo_event(struct seq_oss_devinfo *dp, union evrec *rec, struct snd_seq_event *ev);
  42. /*
  43. * convert an OSS event to ALSA event
  44. * return 0 : enqueued
  45. * non-zero : invalid - ignored
  46. */
  47. int
  48. snd_seq_oss_process_event(struct seq_oss_devinfo *dp, union evrec *q, struct snd_seq_event *ev)
  49. {
  50. switch (q->s.code) {
  51. case SEQ_EXTENDED:
  52. return extended_event(dp, q, ev);
  53. case EV_CHN_VOICE:
  54. return chn_voice_event(dp, q, ev);
  55. case EV_CHN_COMMON:
  56. return chn_common_event(dp, q, ev);
  57. case EV_TIMING:
  58. return timing_event(dp, q, ev);
  59. case EV_SEQ_LOCAL:
  60. return local_event(dp, q, ev);
  61. case EV_SYSEX:
  62. return snd_seq_oss_synth_sysex(dp, q->x.dev, q->x.buf, ev);
  63. case SEQ_MIDIPUTC:
  64. if (dp->seq_mode == SNDRV_SEQ_OSS_MODE_MUSIC)
  65. return -EINVAL;
  66. /* put a midi byte */
  67. if (! is_write_mode(dp->file_mode))
  68. break;
  69. if (snd_seq_oss_midi_open(dp, q->s.dev, SNDRV_SEQ_OSS_FILE_WRITE))
  70. break;
  71. if (snd_seq_oss_midi_filemode(dp, q->s.dev) & SNDRV_SEQ_OSS_FILE_WRITE)
  72. return snd_seq_oss_midi_putc(dp, q->s.dev, q->s.parm1, ev);
  73. break;
  74. case SEQ_ECHO:
  75. if (dp->seq_mode == SNDRV_SEQ_OSS_MODE_MUSIC)
  76. return -EINVAL;
  77. return set_echo_event(dp, q, ev);
  78. case SEQ_PRIVATE:
  79. if (dp->seq_mode == SNDRV_SEQ_OSS_MODE_MUSIC)
  80. return -EINVAL;
  81. return snd_seq_oss_synth_raw_event(dp, q->c[1], q->c, ev);
  82. default:
  83. if (dp->seq_mode == SNDRV_SEQ_OSS_MODE_MUSIC)
  84. return -EINVAL;
  85. return old_event(dp, q, ev);
  86. }
  87. return -EINVAL;
  88. }
  89. /* old type events: mode1 only */
  90. static int
  91. old_event(struct seq_oss_devinfo *dp, union evrec *q, struct snd_seq_event *ev)
  92. {
  93. switch (q->s.code) {
  94. case SEQ_NOTEOFF:
  95. return note_off_event(dp, 0, q->n.chn, q->n.note, q->n.vel, ev);
  96. case SEQ_NOTEON:
  97. return note_on_event(dp, 0, q->n.chn, q->n.note, q->n.vel, ev);
  98. case SEQ_WAIT:
  99. /* skip */
  100. break;
  101. case SEQ_PGMCHANGE:
  102. return set_control_event(dp, 0, SNDRV_SEQ_EVENT_PGMCHANGE,
  103. q->n.chn, 0, q->n.note, ev);
  104. case SEQ_SYNCTIMER:
  105. return snd_seq_oss_timer_reset(dp->timer);
  106. }
  107. return -EINVAL;
  108. }
  109. /* 8bytes extended event: mode1 only */
  110. static int
  111. extended_event(struct seq_oss_devinfo *dp, union evrec *q, struct snd_seq_event *ev)
  112. {
  113. int val;
  114. switch (q->e.cmd) {
  115. case SEQ_NOTEOFF:
  116. return note_off_event(dp, q->e.dev, q->e.chn, q->e.p1, q->e.p2, ev);
  117. case SEQ_NOTEON:
  118. return note_on_event(dp, q->e.dev, q->e.chn, q->e.p1, q->e.p2, ev);
  119. case SEQ_PGMCHANGE:
  120. return set_control_event(dp, q->e.dev, SNDRV_SEQ_EVENT_PGMCHANGE,
  121. q->e.chn, 0, q->e.p1, ev);
  122. case SEQ_AFTERTOUCH:
  123. return set_control_event(dp, q->e.dev, SNDRV_SEQ_EVENT_CHANPRESS,
  124. q->e.chn, 0, q->e.p1, ev);
  125. case SEQ_BALANCE:
  126. /* convert -128:127 to 0:127 */
  127. val = (char)q->e.p1;
  128. val = (val + 128) / 2;
  129. return set_control_event(dp, q->e.dev, SNDRV_SEQ_EVENT_CONTROLLER,
  130. q->e.chn, CTL_PAN, val, ev);
  131. case SEQ_CONTROLLER:
  132. val = ((short)q->e.p3 << 8) | (short)q->e.p2;
  133. switch (q->e.p1) {
  134. case CTRL_PITCH_BENDER: /* SEQ1 V2 control */
  135. /* -0x2000:0x1fff */
  136. return set_control_event(dp, q->e.dev,
  137. SNDRV_SEQ_EVENT_PITCHBEND,
  138. q->e.chn, 0, val, ev);
  139. case CTRL_PITCH_BENDER_RANGE:
  140. /* conversion: 100/semitone -> 128/semitone */
  141. return set_control_event(dp, q->e.dev,
  142. SNDRV_SEQ_EVENT_REGPARAM,
  143. q->e.chn, 0, val*128/100, ev);
  144. default:
  145. return set_control_event(dp, q->e.dev,
  146. SNDRV_SEQ_EVENT_CONTROL14,
  147. q->e.chn, q->e.p1, val, ev);
  148. }
  149. case SEQ_VOLMODE:
  150. return snd_seq_oss_synth_raw_event(dp, q->e.dev, q->c, ev);
  151. }
  152. return -EINVAL;
  153. }
  154. /* channel voice events: mode1 and 2 */
  155. static int
  156. chn_voice_event(struct seq_oss_devinfo *dp, union evrec *q, struct snd_seq_event *ev)
  157. {
  158. if (q->v.chn >= 32)
  159. return -EINVAL;
  160. switch (q->v.cmd) {
  161. case MIDI_NOTEON:
  162. return note_on_event(dp, q->v.dev, q->v.chn, q->v.note, q->v.parm, ev);
  163. case MIDI_NOTEOFF:
  164. return note_off_event(dp, q->v.dev, q->v.chn, q->v.note, q->v.parm, ev);
  165. case MIDI_KEY_PRESSURE:
  166. return set_note_event(dp, q->v.dev, SNDRV_SEQ_EVENT_KEYPRESS,
  167. q->v.chn, q->v.note, q->v.parm, ev);
  168. }
  169. return -EINVAL;
  170. }
  171. /* channel common events: mode1 and 2 */
  172. static int
  173. chn_common_event(struct seq_oss_devinfo *dp, union evrec *q, struct snd_seq_event *ev)
  174. {
  175. if (q->l.chn >= 32)
  176. return -EINVAL;
  177. switch (q->l.cmd) {
  178. case MIDI_PGM_CHANGE:
  179. return set_control_event(dp, q->l.dev, SNDRV_SEQ_EVENT_PGMCHANGE,
  180. q->l.chn, 0, q->l.p1, ev);
  181. case MIDI_CTL_CHANGE:
  182. return set_control_event(dp, q->l.dev, SNDRV_SEQ_EVENT_CONTROLLER,
  183. q->l.chn, q->l.p1, q->l.val, ev);
  184. case MIDI_PITCH_BEND:
  185. /* conversion: 0:0x3fff -> -0x2000:0x1fff */
  186. return set_control_event(dp, q->l.dev, SNDRV_SEQ_EVENT_PITCHBEND,
  187. q->l.chn, 0, q->l.val - 8192, ev);
  188. case MIDI_CHN_PRESSURE:
  189. return set_control_event(dp, q->l.dev, SNDRV_SEQ_EVENT_CHANPRESS,
  190. q->l.chn, 0, q->l.val, ev);
  191. }
  192. return -EINVAL;
  193. }
  194. /* timer events: mode1 and mode2 */
  195. static int
  196. timing_event(struct seq_oss_devinfo *dp, union evrec *q, struct snd_seq_event *ev)
  197. {
  198. switch (q->t.cmd) {
  199. case TMR_ECHO:
  200. if (dp->seq_mode == SNDRV_SEQ_OSS_MODE_MUSIC)
  201. return set_echo_event(dp, q, ev);
  202. else {
  203. union evrec tmp;
  204. memset(&tmp, 0, sizeof(tmp));
  205. /* XXX: only for little-endian! */
  206. tmp.echo = (q->t.time << 8) | SEQ_ECHO;
  207. return set_echo_event(dp, &tmp, ev);
  208. }
  209. case TMR_STOP:
  210. if (dp->seq_mode)
  211. return snd_seq_oss_timer_stop(dp->timer);
  212. return 0;
  213. case TMR_CONTINUE:
  214. if (dp->seq_mode)
  215. return snd_seq_oss_timer_continue(dp->timer);
  216. return 0;
  217. case TMR_TEMPO:
  218. if (dp->seq_mode)
  219. return snd_seq_oss_timer_tempo(dp->timer, q->t.time);
  220. return 0;
  221. }
  222. return -EINVAL;
  223. }
  224. /* local events: mode1 and 2 */
  225. static int
  226. local_event(struct seq_oss_devinfo *dp, union evrec *q, struct snd_seq_event *ev)
  227. {
  228. return -EINVAL;
  229. }
  230. /*
  231. * process note-on event for OSS synth
  232. * three different modes are available:
  233. * - SNDRV_SEQ_OSS_PROCESS_EVENTS (for one-voice per channel mode)
  234. * Accept note 255 as volume change.
  235. * - SNDRV_SEQ_OSS_PASS_EVENTS
  236. * Pass all events to lowlevel driver anyway
  237. * - SNDRV_SEQ_OSS_PROCESS_KEYPRESS (mostly for Emu8000)
  238. * Use key-pressure if note >= 128
  239. */
  240. static int
  241. note_on_event(struct seq_oss_devinfo *dp, int dev, int ch, int note, int vel, struct snd_seq_event *ev)
  242. {
  243. struct seq_oss_synthinfo *info = &dp->synths[dev];
  244. switch (info->arg.event_passing) {
  245. case SNDRV_SEQ_OSS_PROCESS_EVENTS:
  246. if (! info->ch || ch < 0 || ch >= info->nr_voices) {
  247. /* pass directly */
  248. return set_note_event(dp, dev, SNDRV_SEQ_EVENT_NOTEON, ch, note, vel, ev);
  249. }
  250. if (note == 255 && info->ch[ch].note >= 0) {
  251. /* volume control */
  252. int type;
  253. //if (! vel)
  254. /* set volume to zero -- note off */
  255. // type = SNDRV_SEQ_EVENT_NOTEOFF;
  256. //else
  257. if (info->ch[ch].vel)
  258. /* sample already started -- volume change */
  259. type = SNDRV_SEQ_EVENT_KEYPRESS;
  260. else
  261. /* sample not started -- start now */
  262. type = SNDRV_SEQ_EVENT_NOTEON;
  263. info->ch[ch].vel = vel;
  264. return set_note_event(dp, dev, type, ch, info->ch[ch].note, vel, ev);
  265. } else if (note >= 128)
  266. return -EINVAL; /* invalid */
  267. if (note != info->ch[ch].note && info->ch[ch].note >= 0)
  268. /* note changed - note off at beginning */
  269. set_note_event(dp, dev, SNDRV_SEQ_EVENT_NOTEOFF, ch, info->ch[ch].note, 0, ev);
  270. /* set current status */
  271. info->ch[ch].note = note;
  272. info->ch[ch].vel = vel;
  273. if (vel) /* non-zero velocity - start the note now */
  274. return set_note_event(dp, dev, SNDRV_SEQ_EVENT_NOTEON, ch, note, vel, ev);
  275. return -EINVAL;
  276. case SNDRV_SEQ_OSS_PASS_EVENTS:
  277. /* pass the event anyway */
  278. return set_note_event(dp, dev, SNDRV_SEQ_EVENT_NOTEON, ch, note, vel, ev);
  279. case SNDRV_SEQ_OSS_PROCESS_KEYPRESS:
  280. if (note >= 128) /* key pressure: shifted by 128 */
  281. return set_note_event(dp, dev, SNDRV_SEQ_EVENT_KEYPRESS, ch, note - 128, vel, ev);
  282. else /* normal note-on event */
  283. return set_note_event(dp, dev, SNDRV_SEQ_EVENT_NOTEON, ch, note, vel, ev);
  284. }
  285. return -EINVAL;
  286. }
  287. /*
  288. * process note-off event for OSS synth
  289. */
  290. static int
  291. note_off_event(struct seq_oss_devinfo *dp, int dev, int ch, int note, int vel, struct snd_seq_event *ev)
  292. {
  293. struct seq_oss_synthinfo *info = &dp->synths[dev];
  294. switch (info->arg.event_passing) {
  295. case SNDRV_SEQ_OSS_PROCESS_EVENTS:
  296. if (! info->ch || ch < 0 || ch >= info->nr_voices) {
  297. /* pass directly */
  298. return set_note_event(dp, dev, SNDRV_SEQ_EVENT_NOTEON, ch, note, vel, ev);
  299. }
  300. if (info->ch[ch].note >= 0) {
  301. note = info->ch[ch].note;
  302. info->ch[ch].vel = 0;
  303. info->ch[ch].note = -1;
  304. return set_note_event(dp, dev, SNDRV_SEQ_EVENT_NOTEOFF, ch, note, vel, ev);
  305. }
  306. return -EINVAL; /* invalid */
  307. case SNDRV_SEQ_OSS_PASS_EVENTS:
  308. case SNDRV_SEQ_OSS_PROCESS_KEYPRESS:
  309. /* pass the event anyway */
  310. return set_note_event(dp, dev, SNDRV_SEQ_EVENT_NOTEOFF, ch, note, vel, ev);
  311. }
  312. return -EINVAL;
  313. }
  314. /*
  315. * create a note event
  316. */
  317. static int
  318. set_note_event(struct seq_oss_devinfo *dp, int dev, int type, int ch, int note, int vel, struct snd_seq_event *ev)
  319. {
  320. if (! snd_seq_oss_synth_is_valid(dp, dev))
  321. return -ENXIO;
  322. ev->type = type;
  323. snd_seq_oss_synth_addr(dp, dev, ev);
  324. ev->data.note.channel = ch;
  325. ev->data.note.note = note;
  326. ev->data.note.velocity = vel;
  327. return 0;
  328. }
  329. /*
  330. * create a control event
  331. */
  332. static int
  333. set_control_event(struct seq_oss_devinfo *dp, int dev, int type, int ch, int param, int val, struct snd_seq_event *ev)
  334. {
  335. if (! snd_seq_oss_synth_is_valid(dp, dev))
  336. return -ENXIO;
  337. ev->type = type;
  338. snd_seq_oss_synth_addr(dp, dev, ev);
  339. ev->data.control.channel = ch;
  340. ev->data.control.param = param;
  341. ev->data.control.value = val;
  342. return 0;
  343. }
  344. /*
  345. * create an echo event
  346. */
  347. static int
  348. set_echo_event(struct seq_oss_devinfo *dp, union evrec *rec, struct snd_seq_event *ev)
  349. {
  350. ev->type = SNDRV_SEQ_EVENT_ECHO;
  351. /* echo back to itself */
  352. snd_seq_oss_fill_addr(dp, ev, dp->addr.client, dp->addr.port);
  353. memcpy(&ev->data, rec, LONG_EVENT_SIZE);
  354. return 0;
  355. }
  356. /*
  357. * event input callback from ALSA sequencer:
  358. * the echo event is processed here.
  359. */
  360. int
  361. snd_seq_oss_event_input(struct snd_seq_event *ev, int direct, void *private_data,
  362. int atomic, int hop)
  363. {
  364. struct seq_oss_devinfo *dp = (struct seq_oss_devinfo *)private_data;
  365. union evrec *rec;
  366. if (ev->type != SNDRV_SEQ_EVENT_ECHO)
  367. return snd_seq_oss_midi_input(ev, direct, private_data);
  368. if (ev->source.client != dp->cseq)
  369. return 0; /* ignored */
  370. rec = (union evrec*)&ev->data;
  371. if (rec->s.code == SEQ_SYNCTIMER) {
  372. /* sync echo back */
  373. snd_seq_oss_writeq_wakeup(dp->writeq, rec->t.time);
  374. } else {
  375. /* echo back event */
  376. if (dp->readq == NULL)
  377. return 0;
  378. snd_seq_oss_readq_put_event(dp->readq, rec);
  379. }
  380. return 0;
  381. }