seq_oss_event.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458
  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;
  244. if (!snd_seq_oss_synth_is_valid(dp, dev))
  245. return -ENXIO;
  246. info = &dp->synths[dev];
  247. switch (info->arg.event_passing) {
  248. case SNDRV_SEQ_OSS_PROCESS_EVENTS:
  249. if (! info->ch || ch < 0 || ch >= info->nr_voices) {
  250. /* pass directly */
  251. return set_note_event(dp, dev, SNDRV_SEQ_EVENT_NOTEON, ch, note, vel, ev);
  252. }
  253. if (note == 255 && info->ch[ch].note >= 0) {
  254. /* volume control */
  255. int type;
  256. //if (! vel)
  257. /* set volume to zero -- note off */
  258. // type = SNDRV_SEQ_EVENT_NOTEOFF;
  259. //else
  260. if (info->ch[ch].vel)
  261. /* sample already started -- volume change */
  262. type = SNDRV_SEQ_EVENT_KEYPRESS;
  263. else
  264. /* sample not started -- start now */
  265. type = SNDRV_SEQ_EVENT_NOTEON;
  266. info->ch[ch].vel = vel;
  267. return set_note_event(dp, dev, type, ch, info->ch[ch].note, vel, ev);
  268. } else if (note >= 128)
  269. return -EINVAL; /* invalid */
  270. if (note != info->ch[ch].note && info->ch[ch].note >= 0)
  271. /* note changed - note off at beginning */
  272. set_note_event(dp, dev, SNDRV_SEQ_EVENT_NOTEOFF, ch, info->ch[ch].note, 0, ev);
  273. /* set current status */
  274. info->ch[ch].note = note;
  275. info->ch[ch].vel = vel;
  276. if (vel) /* non-zero velocity - start the note now */
  277. return set_note_event(dp, dev, SNDRV_SEQ_EVENT_NOTEON, ch, note, vel, ev);
  278. return -EINVAL;
  279. case SNDRV_SEQ_OSS_PASS_EVENTS:
  280. /* pass the event anyway */
  281. return set_note_event(dp, dev, SNDRV_SEQ_EVENT_NOTEON, ch, note, vel, ev);
  282. case SNDRV_SEQ_OSS_PROCESS_KEYPRESS:
  283. if (note >= 128) /* key pressure: shifted by 128 */
  284. return set_note_event(dp, dev, SNDRV_SEQ_EVENT_KEYPRESS, ch, note - 128, vel, ev);
  285. else /* normal note-on event */
  286. return set_note_event(dp, dev, SNDRV_SEQ_EVENT_NOTEON, ch, note, vel, ev);
  287. }
  288. return -EINVAL;
  289. }
  290. /*
  291. * process note-off event for OSS synth
  292. */
  293. static int
  294. note_off_event(struct seq_oss_devinfo *dp, int dev, int ch, int note, int vel, struct snd_seq_event *ev)
  295. {
  296. struct seq_oss_synthinfo *info;
  297. if (!snd_seq_oss_synth_is_valid(dp, dev))
  298. return -ENXIO;
  299. info = &dp->synths[dev];
  300. switch (info->arg.event_passing) {
  301. case SNDRV_SEQ_OSS_PROCESS_EVENTS:
  302. if (! info->ch || ch < 0 || ch >= info->nr_voices) {
  303. /* pass directly */
  304. return set_note_event(dp, dev, SNDRV_SEQ_EVENT_NOTEON, ch, note, vel, ev);
  305. }
  306. if (info->ch[ch].note >= 0) {
  307. note = info->ch[ch].note;
  308. info->ch[ch].vel = 0;
  309. info->ch[ch].note = -1;
  310. return set_note_event(dp, dev, SNDRV_SEQ_EVENT_NOTEOFF, ch, note, vel, ev);
  311. }
  312. return -EINVAL; /* invalid */
  313. case SNDRV_SEQ_OSS_PASS_EVENTS:
  314. case SNDRV_SEQ_OSS_PROCESS_KEYPRESS:
  315. /* pass the event anyway */
  316. return set_note_event(dp, dev, SNDRV_SEQ_EVENT_NOTEOFF, ch, note, vel, ev);
  317. }
  318. return -EINVAL;
  319. }
  320. /*
  321. * create a note event
  322. */
  323. static int
  324. set_note_event(struct seq_oss_devinfo *dp, int dev, int type, int ch, int note, int vel, struct snd_seq_event *ev)
  325. {
  326. if (! snd_seq_oss_synth_is_valid(dp, dev))
  327. return -ENXIO;
  328. ev->type = type;
  329. snd_seq_oss_synth_addr(dp, dev, ev);
  330. ev->data.note.channel = ch;
  331. ev->data.note.note = note;
  332. ev->data.note.velocity = vel;
  333. return 0;
  334. }
  335. /*
  336. * create a control event
  337. */
  338. static int
  339. set_control_event(struct seq_oss_devinfo *dp, int dev, int type, int ch, int param, int val, struct snd_seq_event *ev)
  340. {
  341. if (! snd_seq_oss_synth_is_valid(dp, dev))
  342. return -ENXIO;
  343. ev->type = type;
  344. snd_seq_oss_synth_addr(dp, dev, ev);
  345. ev->data.control.channel = ch;
  346. ev->data.control.param = param;
  347. ev->data.control.value = val;
  348. return 0;
  349. }
  350. /*
  351. * create an echo event
  352. */
  353. static int
  354. set_echo_event(struct seq_oss_devinfo *dp, union evrec *rec, struct snd_seq_event *ev)
  355. {
  356. ev->type = SNDRV_SEQ_EVENT_ECHO;
  357. /* echo back to itself */
  358. snd_seq_oss_fill_addr(dp, ev, dp->addr.client, dp->addr.port);
  359. memcpy(&ev->data, rec, LONG_EVENT_SIZE);
  360. return 0;
  361. }
  362. /*
  363. * event input callback from ALSA sequencer:
  364. * the echo event is processed here.
  365. */
  366. int
  367. snd_seq_oss_event_input(struct snd_seq_event *ev, int direct, void *private_data,
  368. int atomic, int hop)
  369. {
  370. struct seq_oss_devinfo *dp = (struct seq_oss_devinfo *)private_data;
  371. union evrec *rec;
  372. if (ev->type != SNDRV_SEQ_EVENT_ECHO)
  373. return snd_seq_oss_midi_input(ev, direct, private_data);
  374. if (ev->source.client != dp->cseq)
  375. return 0; /* ignored */
  376. rec = (union evrec*)&ev->data;
  377. if (rec->s.code == SEQ_SYNCTIMER) {
  378. /* sync echo back */
  379. snd_seq_oss_writeq_wakeup(dp->writeq, rec->t.time);
  380. } else {
  381. /* echo back event */
  382. if (dp->readq == NULL)
  383. return 0;
  384. snd_seq_oss_readq_put_event(dp->readq, rec);
  385. }
  386. return 0;
  387. }