~ubuntu-branches/ubuntu/oneiric/oss4/oneiric-proposed

« back to all changes in this revision

Viewing changes to tutorials/sndkit/samples/midi.c

  • Committer: Bazaar Package Importer
  • Author(s): Stefano Rivera
  • Date: 2011-06-16 20:37:48 UTC
  • mfrom: (5.1.3 sid)
  • Revision ID: james.westby@ubuntu.com-20110616203748-jbrxik6ql33z54co
Tags: 4.2-build2004-1ubuntu1
* Merge from Debian unstable.
  - Supports our current kernel (LP: #746048)
  Remaining changes:
  - debian/oss4-dkms.dkms.in: s/source/build/ in Kernel headers paths.
* ld-as-needed.patch: Re-order CC arguments to enable building with ld
  --as-needed (LP: #770972)

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*
 
2
 * Purpose: A minimalistic MIDI output programming sample.
 
3
 * Copyright (C) 4Front Technologies, 2002-2004. Released under GPLv2/CDDL.
 
4
 *
 
5
 * Description:
 
6
 * This program does nothing but plays a note on MIDI channel 1
 
7
 * after sending a program change message.
 
8
 *
 
9
 * This program demonstrates how simple it's to write programs that play
 
10
 * MIDI. All you need to do is assembling the MIDI message and writing 
 
11
 * it to the device. The MIDI format is defined  in "MIDI 1.0 Detailed
 
12
 * Specification" which is available from MIDI Manufacturs Association (MMA)
 
13
 * (see {!hlink http://www.midi.org}).
 
14
 *
 
15
 * This program does timing by calling the sleep(3) system call. In some
 
16
 * systems like Linux there may be better sleep routines like usleep(3)
 
17
 * that provide better timing resolution.
 
18
 *
 
19
 * However application based timing may not be as precise as required in
 
20
 * musical applications. For this reason the MIDI interface of OSS will
 
21
 * provide a driver based timing approach in the near future.
 
22
 *
 
23
 * Please look at the "{!link MIDI}" section of the OSS Developer's
 
24
 * manual for more info about MIDI programming.
 
25
 */
 
26
 
 
27
#include <stdio.h>
 
28
#include <unistd.h>
 
29
#include <stdlib.h>
 
30
#include <fcntl.h>
 
31
#include <soundcard.h>
 
32
 
 
33
#define DEVICE "/dev/midi"
 
34
 
 
35
int
 
36
main ()
 
37
{
 
38
  int fd;
 
39
 
 
40
  unsigned char note_on[] = { 0xc0, 0,  /* Program change */
 
41
    0x90, 60, 60
 
42
  };                            /* Note on */
 
43
  unsigned char note_off[] = { 0x80, 60, 60 };  /* Note off */
 
44
 
 
45
  if ((fd = open (DEVICE, O_WRONLY, 0)) == -1)
 
46
    {
 
47
      perror ("open " DEVICE);
 
48
      exit (-1);
 
49
    }
 
50
 
 
51
  if (write (fd, note_on, sizeof (note_on)) == -1)
 
52
    {
 
53
      perror ("write " DEVICE);
 
54
      exit (-1);
 
55
    }
 
56
 
 
57
  sleep (1);                    /* Delay one second */
 
58
 
 
59
  if (write (fd, note_off, sizeof (note_off)) == -1)
 
60
    {
 
61
      perror ("write " DEVICE);
 
62
      exit (-1);
 
63
    }
 
64
 
 
65
  close (fd);
 
66
  exit (0);
 
67
}