XEmacs 21.2.33 "Melpomene".
[chise/xemacs-chise.git.1] / src / ntplay.c
1 /* Sound in windows nt XEmacs.
2    Copyright (C) 1998 Andy Piper.
3
4 This file is part of XEmacs.
5
6 XEmacs is free software; you can redistribute it and/or modify it
7 under the terms of the GNU General Public License as published by the
8 Free Software Foundation; either version 2, or (at your option) any
9 later version.
10
11 XEmacs is distributed in the hope that it will be useful, but WITHOUT
12 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
13 FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
14 for more details.
15
16 You should have received a copy of the GNU General Public License
17 along with XEmacs; see the file COPYING.  If not, write to the Free
18 Software Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
19 02111-1307, USA.*/
20
21 #include <windows.h>
22 #include <config.h>
23 #include <stdio.h>
24 #include "sysfile.h"
25 #include "lisp.h"
26 #include "nativesound.h"
27
28 #if (defined (__CYGWIN32__) || defined(__MINGW32__)) && \
29         CYGWIN_VERSION_DLL_MAJOR < 21
30 extern BOOL WINAPI PlaySound(LPCSTR,HMODULE,DWORD);
31 #else
32 #include <mmsystem.h>
33 #endif
34 static int play_sound_data_1 (unsigned char *data, int length,
35                                int volume, int convert);
36
37 void play_sound_file (char *sound_file, int volume)
38 {
39   DWORD flags = SND_ASYNC | SND_NODEFAULT | SND_FILENAME;
40   OFSTRUCT ofs;
41   Lisp_Object fname = Ffile_name_nondirectory (build_string (sound_file));
42
43   CHECK_STRING (fname);
44   if (OpenFile (XSTRING_DATA (fname), &ofs, OF_EXIST) < 0)
45     {
46       /* file isn't in the path so read it as data */
47       int size;
48       unsigned char* data;
49       int ofd = open (sound_file, O_RDONLY | OPEN_BINARY, 0);
50       
51       if (ofd <0)
52         return;
53
54       size = lseek (ofd, 0, SEEK_END);
55       data = (unsigned char *)xmalloc (size);
56       lseek (ofd, 0, SEEK_SET);
57       
58       if (!data)
59         {
60           close (ofd);
61           return;
62         }
63
64       if (read (ofd, data, size) != size)
65         {
66           close (ofd);
67           xfree (data);
68           return;
69         }
70       close (ofd);
71       
72       play_sound_data_1 (data, size, 100, FALSE);
73     }
74   else 
75     PlaySound (XSTRING_DATA (fname), NULL, flags);
76 }
77
78 /* mswindows can't cope with playing a sound from alloca space so we
79    have to convert if necessary */
80 static int play_sound_data_1 (unsigned char *data, int length, int volume,
81                                int convert_to_malloc)
82 {
83   DWORD flags = SND_ASYNC | SND_MEMORY | SND_NODEFAULT;
84   static unsigned char* sound_data=0;
85   if (sound_data)
86     {
87       PlaySound (NULL, NULL, flags);
88       xfree (sound_data);
89       sound_data=0;
90     }
91
92   if (convert_to_malloc)
93     {
94       sound_data = (unsigned char *)xmalloc (length);
95       memcpy (sound_data, data, length);
96     }
97   else
98     sound_data = data;
99
100   PlaySound(sound_data, NULL, flags);
101
102   /* #### Error handling? */ 
103   return 1;
104 }
105
106 int play_sound_data (unsigned char *data, int length, int volume)
107 {
108   return play_sound_data_1 (data, length, volume, TRUE);
109 }