File: dossound.c

package info (click to toggle)
atari800 0.9.8a-2
  • links: PTS
  • area: contrib
  • in suites: potato, slink
  • size: 1,844 kB
  • ctags: 3,008
  • sloc: ansic: 29,881; asm: 2,142; makefile: 78; sh: 8
file content (130 lines) | stat: -rw-r--r-- 2,907 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
#include "config.h"
#include <fcntl.h>
#include "atari.h"
#include "pokeysnd.h"
#include "allegro.h"
extern int timesync;
static char *rcsid = "$Id: sound.c,v 1.2 1997/04/18 22:23:38 david Exp $";
int speed_count = 0;

#define FALSE 0
#define TRUE 1
static sound_enabled = TRUE;
static int gain = 4;

static int AUDCTL = 0x00;
static int AUDF[4] =
{0, 0, 0, 0};
static int AUDC[4] =
{0, 0, 0, 0};

static int sample_pos;
static unsigned char *buffer;
static int frames_per_second = 60;
static int real_updates_per_frame = 262;
static int stream_len;
static int stream_freq;
AUDIOSTREAM *stream;
   
void init_stream(){
   stream = play_audio_stream(stream_len, 8, stream_freq , 255, 128);//255=vol 128=pan 8=bits
   if (!stream) {
      printf("Error creating audio stream!\n");
      abort();
   }
}
static int updatecount;
void pokey_update(void)
{
	int newpos;
	if (sound_enabled == 0)
		return;
	if (updatecount >= real_updates_per_frame)
		return;
	newpos = stream_len * (updatecount) / real_updates_per_frame;
	if (newpos > stream_len)
		newpos = stream_len;

	Pokey_process(buffer + sample_pos, newpos - sample_pos);
	sample_pos = newpos;
	updatecount++;
}

int dossound_Initialise(int *argc, char *argv[])
{
	int i, j;
	if (tv_mode == TV_PAL) {
		frames_per_second = 50;
		real_updates_per_frame = 312;
	}
	for (i = j = 1; i < *argc; i++) {
		if (stricmp(argv[i], "-sound") == 0)
			sound_enabled = TRUE;
		else if (strcmp(argv[i], "-nosound") == 0)
			sound_enabled = FALSE;
		else
			argv[j++] = argv[i];
	}
	*argc = j;

	if (sound_enabled) {
         if (install_sound(DIGI_AUTODETECT, MIDI_NONE, NULL) != 0) {
            printf("Error initialising sound system\n%s\n", allegro_error);
            return 1;
         }
	}
	if (sound_enabled) {
		timesync = FALSE;
		stream_len = (44100 - 1) / frames_per_second;
		stream_freq = stream_len * frames_per_second;
		sample_pos = 0;
		if ((buffer = (char *) malloc(stream_len)) == 0) {
			free(buffer);
			return 1;
		}
		memset(buffer, 0x00, stream_len);
		Pokey_sound_init(FREQ_17_EXACT, stream_freq, 1);
                init_stream();
	}
	else {
		timesync = TRUE;
	}
	return 0;
}

void dossound_Exit(void)
{
}

void dossound_UpdateSound(void)
{
	if (sound_enabled) {
             	unsigned char *p;
                updatecount = 0;
                if (sample_pos < stream_len)
		   Pokey_process(buffer + sample_pos, stream_len - sample_pos);
                sample_pos = 0;
                while (!(p=get_audio_stream_buffer(stream))){}
                memcpy(p, buffer, stream_len);
                free_audio_stream_buffer(stream);
	}
}

void Atari_AUDC(int channel, int byte)
{
	channel--;
	Update_pokey_sound(  1 + channel + channel, byte, 0, gain);
}

void Atari_AUDF(int channel, int byte)
{
	channel--;
	Update_pokey_sound(  channel + channel, byte, 0, gain);
}

void Atari_AUDCTL(int byte)
{
	Update_pokey_sound(  8, byte, 0, gain);
}