PCem

view src/sound.c @ 117:a6ce8addfffa

Fixed crash when selecting no sound card. Fixed number of heads when creating a new hard disc for drive C.
author TomW
date Tue Jul 08 21:03:34 2014 +0100
parents f749363ad763
children
line source
1 #include <stdio.h>
2 #include <stdlib.h>
3 #include "ibm.h"
4 #include "device.h"
6 #include "filters.h"
8 #include "sound_opl.h"
10 #include "sound.h"
11 #include "sound_adlib.h"
12 #include "sound_adlibgold.h"
13 #include "sound_pas16.h"
14 #include "sound_sb.h"
15 #include "sound_sb_dsp.h"
16 #include "sound_wss.h"
18 #include "timer.h"
20 int sound_card_current = 0;
21 static int sound_card_last = 0;
23 typedef struct
24 {
25 char name[32];
26 device_t *device;
27 } SOUND_CARD;
29 static SOUND_CARD sound_cards[] =
30 {
31 {"None", NULL},
32 {"Adlib", &adlib_device},
33 {"Sound Blaster 1.0", &sb_1_device},
34 {"Sound Blaster 1.5", &sb_15_device},
35 {"Sound Blaster 2.0", &sb_2_device},
36 {"Sound Blaster Pro v1", &sb_pro_v1_device},
37 {"Sound Blaster Pro v2", &sb_pro_v2_device},
38 {"Sound Blaster 16", &sb_16_device},
39 {"Sound Blaster AWE32", &sb_awe32_device},
40 {"Adlib Gold", &adgold_device},
41 {"Windows Sound System", &wss_device},
42 {"Pro Audio Spectrum 16", &pas16_device},
43 {"", NULL}
44 };
46 int sound_card_available(int card)
47 {
48 if (sound_cards[card].device)
49 return device_available(sound_cards[card].device);
51 return 1;
52 }
54 char *sound_card_getname(int card)
55 {
56 return sound_cards[card].name;
57 }
59 device_t *sound_card_getdevice(int card)
60 {
61 return sound_cards[card].device;
62 }
64 int sound_card_has_config(int card)
65 {
66 if (!sound_cards[card].device)
67 return 0;
68 return sound_cards[card].device->config ? 1 : 0;
69 }
71 void sound_card_init()
72 {
73 if (sound_cards[sound_card_current].device)
74 device_add(sound_cards[sound_card_current].device);
75 sound_card_last = sound_card_current;
76 }
78 static struct
79 {
80 void (*poll)(void *p);
81 void (*get_buffer)(int16_t *buffer, int len, void *p);
82 void *priv;
83 } sound_handlers[8];
85 static int sound_handlers_num;
87 static int sound_poll_time = 0, sound_get_buffer_time = 0;
89 int soundon = 1;
92 static int16_t cd_buffer[SOUNDBUFLEN * 2];
94 void sound_cd_poll(void *p)
95 {
96 }
98 void sound_cd_get_buffer(int16_t *buffer, int len, void *p)
99 {
100 int pos, c;
101 ioctl_audio_callback(cd_buffer, (len * 2 * 441) / 480);
102 pos = 0;
104 for (c = 0; c < len * 2; c+=2)
105 {
106 buffer[c] += cd_buffer[((pos >> 16) << 1)] / 2;
107 buffer[c + 1] += cd_buffer[((pos >> 16) << 1) + 1] / 2;
108 pos += 60211; //(44100 * 65536) / 48000;
109 }
110 }
112 static uint16_t *outbuffer;
114 void sound_init()
115 {
116 initalmain(0,NULL);
117 inital();
119 outbuffer = malloc(SOUNDBUFLEN * 2 * sizeof(int16_t));
121 sound_add_handler(sound_cd_poll, sound_cd_get_buffer, NULL);
122 }
124 void sound_add_handler(void (*poll)(void *p), void (*get_buffer)(int16_t *buffer, int len, void *p), void *p)
125 {
126 sound_handlers[sound_handlers_num].poll = poll;
127 sound_handlers[sound_handlers_num].get_buffer = get_buffer;
128 sound_handlers[sound_handlers_num].priv = p;
129 sound_handlers_num++;
130 }
132 void sound_poll(void *priv)
133 {
134 int c;
136 sound_poll_time += (int)((double)TIMER_USEC * (1000000.0 / 48000.0));
138 for (c = 0; c < sound_handlers_num; c++)
139 sound_handlers[c].poll(sound_handlers[c].priv);
140 }
142 FILE *soundf;
144 void sound_get_buffer(void *priv)
145 {
146 int c;
148 sound_get_buffer_time += (TIMER_USEC * (1000000 / 10));
150 memset(outbuffer, 0, SOUNDBUFLEN * 2 * sizeof(int16_t));
152 for (c = 0; c < sound_handlers_num; c++)
153 sound_handlers[c].get_buffer(outbuffer, SOUNDBUFLEN, sound_handlers[c].priv);
155 /* if (!soundf) soundf=fopen("sound.pcm","wb");
156 fwrite(outbuffer,(SOUNDBUFLEN)*2*2,1,soundf);*/
158 if (soundon) givealbuffer(outbuffer);
159 }
161 void sound_reset()
162 {
163 timer_add(sound_poll, &sound_poll_time, TIMER_ALWAYS_ENABLED, NULL);
164 timer_add(sound_get_buffer, &sound_get_buffer_time, TIMER_ALWAYS_ENABLED, NULL);
166 sound_handlers_num = 0;
167 }