1 ///////////////////////////////////////////////////////////////////////////////
2 // $Id: Sound.cxx,v 1.3 2000/01/10 23:33:06 bwmott Exp $
3 ///////////////////////////////////////////////////////////////////////////////
4 //
5 // Sound.cxx - Sound class
6 //
7 //
8 // Bradford W. Mott
9 // Copyright (C) 1995
10 // January 4,1995
11 //
12 ///////////////////////////////////////////////////////////////////////////////
13 // $Log: Sound.cxx,v $
14 // Revision 1.3  2000/01/10 23:33:06  bwmott
15 // Sound system uses the /dev/dsp device now instead of the /dev/audio device
16 //
17 // Revision 1.2  1996/01/06 05:12:39  bwmott
18 // Changed all NULLs to 0 and added (char*) cast in the write system call
19 //
20 // Revision 1.1  1995/01/08 06:48:24  bmott
21 // Initial revision
22 //
23 ///////////////////////////////////////////////////////////////////////////////
24 
25 #include <sys/types.h>
26 #include <sys/stat.h>
27 #include <fcntl.h>
28 #include <unistd.h>
29 
30 #include "Sound.hxx"
31 
32 
33 ///////////////////////////////////////////////////////////////////////////////
34 // Constructor
35 ///////////////////////////////////////////////////////////////////////////////
Sound(SampleCollection * sampleCollection)36 Sound::Sound(SampleCollection* sampleCollection)
37     : mySampleCollection(sampleCollection)
38 {
39   // Determine if there is a usable sound device
40   int soundDevice = open("/dev/dsp", O_WRONLY, 0);
41 
42   if(soundDevice == -1)
43   {
44     myState = Disabled;
45   }
46   else
47   {
48     myState = Enabled;
49     close(soundDevice);
50   }
51 }
52 
53 ///////////////////////////////////////////////////////////////////////////////
54 // Destructor
55 ///////////////////////////////////////////////////////////////////////////////
~Sound()56 Sound::~Sound()
57 {
58 }
59 
60 ///////////////////////////////////////////////////////////////////////////////
61 // Play the named sample
62 ///////////////////////////////////////////////////////////////////////////////
playSample(char * sampleName)63 void Sound::playSample(char* sampleName)
64 {
65   // Play the sample if I'm enabled
66   if(myState == Enabled)
67   {
68     // Open the sound device
69     int audio = open("/dev/dsp", O_WRONLY, 0);
70 
71     if(audio != -1)
72     {
73       // Get the sample from my sample collection
74       Sample* sample = mySampleCollection->getByName(sampleName);
75 
76       if(sample != 0)
77         write(audio, (char*)sample->data(), sample->length());
78 
79       close(audio);
80     }
81   }
82 }
83 
84