|
|
1.1 root 1: /* Generator is (c) James Ponder, 1997-2001 http://www.squish.net/generator/ */
2:
3: #include <sys/types.h>
4: #include <sys/stat.h>
5: #include <sys/ioctl.h>
6: #include <unistd.h>
7: #include <fcntl.h>
8: #include <errno.h>
9: #include <string.h>
10: #include <math.h>
11:
12: #include "generator.h"
13: #include "gensound.h"
14: #include "gensoundp.h"
15: #include "vdp.h"
16: #include "ui.h"
17:
18: #ifdef JFM
19: # include "jfm.h"
20: #else
21: # include "support.h"
22: # include "fm.h"
23: #endif
24:
25: #include <sys/soundcard.h>
26:
27: #define SOUND_DEVICE "/dev/dsp"
28:
29: /*** variables externed ***/
30:
31: /*** forward references ***/
32:
33: /*** file scoped variables ***/
34:
35: static int soundp_dev = -1;
36: static int soundp_format;
37: static int soundp_stereo;
38: static int soundp_frag;
39: static unsigned int soundp_speed;
40:
41: #ifdef JFM
42: static t_jfm_ctx *sound_ctx;
43: #endif
44:
45: /*** soundp_start - start sound hardware ***/
46:
47: int soundp_start(void)
48: {
49: audio_buf_info sound_info;
50:
51: if ((soundp_dev = open(SOUND_DEVICE, O_WRONLY, 0)) == -1) {
52: LOG_CRITICAL(("open " SOUND_DEVICE " failed: %s", strerror(errno)));
53: return 1;
54: }
55: soundp_frag = (sound_maxfields << 16 |
56: (int)(ceil(log10(sound_sampsperfield * 4) / log10(2))));
57: if (ioctl(soundp_dev, SNDCTL_DSP_SETFRAGMENT, &soundp_frag) == -1) {
58: LOG_CRITICAL(("Error setting fragment size: %s", strerror(errno)));
59: close(soundp_dev);
60: return 1;
61: }
62: soundp_format = AFMT_S16_LE;
63: if (ioctl(soundp_dev, SNDCTL_DSP_SETFMT, &soundp_format) == -1) {
64: LOG_CRITICAL(("Error setting sound device format: %s", strerror(errno)));
65: close(soundp_dev);
66: return 1;
67: }
68: if (soundp_format != AFMT_S16_LE && soundp_format != AFMT_S16_BE) {
69: LOG_CRITICAL(("Sound device format not supported (must be 16 bit)"));
70: close(soundp_dev);
71: return 1;
72: }
73: soundp_stereo = 1;
74: if (ioctl(soundp_dev, SNDCTL_DSP_STEREO, &soundp_stereo) == -1) {
75: LOG_CRITICAL(("Error setting stereo: %s", strerror(errno)));
76: close(soundp_dev);
77: return 1;
78: }
79: if (soundp_stereo != 1) {
80: LOG_CRITICAL(("Sound device does not support stereo"));
81: close(soundp_dev);
82: return 1;
83: }
84: soundp_speed = sound_speed;
85: if (ioctl(soundp_dev, SNDCTL_DSP_SPEED, &soundp_speed) == -1) {
86: LOG_CRITICAL(("Error setting sound speed: %s", strerror(errno)));
87: close(soundp_dev);
88: return 1;
89: }
90: if (soundp_speed != sound_speed) {
91: if (abs(soundp_speed - sound_speed) < 100) {
92: LOG_NORMAL(("Warning: Sample rate not exactly 22050"));
93: } else {
94: LOG_CRITICAL(("Sound device does not support sample rate %d "
95: "(returned %d)", sound_speed, soundp_speed));
96: close(soundp_dev);
97: return 1;
98: }
99: }
100: if (ioctl(soundp_dev, SNDCTL_DSP_GETOSPACE, &sound_info) == -1) {
101: LOG_CRITICAL(("Error getting output space info", strerror(errno)));
102: close(soundp_dev);
103: return 1;
104: }
105: LOG_VERBOSE(("Total allocated fragments = %d (requested %d)",
106: sound_info.fragstotal, sound_maxfields));
107: LOG_VERBOSE(("Fragment size = %d (requested %d)", sound_info.fragsize,
108: sound_sampsperfield * 4));
109: LOG_VERBOSE(("Bytes free in buffer = %d", sound_info.bytes));
110: LOG_VERBOSE(("Theshold = %d bytes (%d fields of sound === %dms latency)",
111: sound_threshold * 4, sound_minfields,
112: (int)(1000 * (float)sound_minfields / (float)vdp_framerate)));
113: if ((signed)sound_threshold >= sound_info.bytes) {
114: LOG_CRITICAL(("Threshold exceeds bytes free in buffer"));
115: close(soundp_dev);
116: return 1;
117: }
118: return 0;
119: }
120:
121: /*** soundp_stop - stop sound hardware ***/
122:
123: void soundp_stop(void)
124: {
125: if (soundp_dev != -1)
126: close(soundp_dev);
127: soundp_dev = -1;
128: }
129:
130: /*** sound_samplesbuffered - how many samples are currently buffered? ***/
131:
132: int soundp_samplesbuffered(void)
133: {
134: audio_buf_info sound_info;
135: int pending;
136:
137: if (ioctl(soundp_dev, SNDCTL_DSP_GETOSPACE, &sound_info) == -1) {
138: LOG_CRITICAL(("Error getting output space info: %s", strerror(errno)));
139: return -1;
140: }
141: pending = (sound_info.fragstotal * sound_info.fragsize) - sound_info.bytes;
142: return (pending / 4); /* 2 bytes per sample, 2 channel stereo */
143: }
144:
145: void soundp_output(uint16 *left, uint16 *right, unsigned int samples)
146: {
147: uint16 buffer[(SOUND_MAXRATE / 50) * 2]; /* pal is slowest framerate */
148: #ifdef WORDS_BIGENDIAN
149: int endian = 1;
150: #else
151: int endian = 0;
152: #endif
153: unsigned int i;
154:
155: if (samples > (sizeof(buffer) << 1)) {
156: /* we only bother with coping with one fields worth of samples */
157: LOG_CRITICAL(("Too many samples passed to soundp_output!"));
158: return;
159: }
160: if (soundp_format == AFMT_S16_LE && endian == 0) {
161: /* device matches endianness of host */
162: for (i = 0; i < samples; i++) {
163: buffer[i * 2] = left[i];
164: buffer[i * 2 + 1] = right[i];
165: }
166: } else {
167: /* device is odd and is different to host endianness */
168: for (i = 0; i < samples; i++) {
169: buffer[i * 2] = ((left[i] >> 8) | ((left[i] << 8) & 0xff00));
170: buffer[i * 2 + 1] = ((right[i] >> 8) | ((right[i] << 8) & 0xff00));
171: }
172: }
173: if (write(soundp_dev, buffer, samples * 4) == -1) {
174: if (errno != EINTR)
175: LOG_CRITICAL(("Error writing to sound device: %s", strerror(errno)));
176: }
177: }
This archive runs on limited infrastructure. Preserving old code on modern bandwidth. Automated agents are requested to crawl responsibly.