seq_oss_event.c 13 KB

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