aboutsummaryrefslogtreecommitdiffstats
path: root/src/jake2/client/SND_JAVA.java
blob: 3853e09464a1fe885e2df78354f06ff153a84104 (plain)
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
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
/*
 * SND_JAVA.java
 * Copyright (C) 2004
 * 
 * $Id: SND_JAVA.java,v 1.1 2004-07-07 19:58:51 hzi Exp $
 */
/*
Copyright (C) 1997-2001 Id Software, Inc.

This program is free software; you can redistribute it and/or
modify it under the terms of the GNU General Public License
as published by the Free Software Foundation; either version 2
of the License, or (at your option) any later version.

This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.

See the GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.

*/
package jake2.client;

import jake2.Globals;
import jake2.game.cvar_t;
import jake2.qcommon.Cvar;
import jake2.qcommon.FS;

import java.io.*;
import java.io.FileInputStream;
import java.io.IOException;

import javax.sound.sampled.*;

/**
 * SND_JAVA
 */
public class SND_JAVA extends Globals {

	static boolean snd_inited= false;

	static cvar_t sndbits;
	static cvar_t sndspeed;
	static cvar_t sndchannels;

//	static int tryrates[] = { 11025, 22051, 44100, 8000 };
	static class dma_t {
		int channels;
		int samples; // mono samples in buffer
		int submission_chunk; // don't mix less than this #
		int samplepos; // in mono samples
		int samplebits;
		int speed;
		byte[] buffer;
	}
  	static SND_DMA.dma_t dma = new dma_t();
  	
	static SourceDataLine line;
	static AudioFormat format;


	static boolean SNDDMA_Init() {

		if (snd_inited)
			return true;

		if (sndbits == null) {
			sndbits = Cvar.Get("sndbits", "16", CVAR_ARCHIVE);
			sndspeed = Cvar.Get("sndspeed", "0", CVAR_ARCHIVE);
			sndchannels = Cvar.Get("sndchannels", "2", CVAR_ARCHIVE);
		}
		
		byte[] sound = FS.LoadFile("sound/misc/menu1.wav");
		AudioInputStream stream;
		try {
			stream = AudioSystem.getAudioInputStream(new ByteArrayInputStream(sound));
		} catch (UnsupportedAudioFileException e) {
			return false;
		} catch (IOException e) {
			return false;
		}
		format = stream.getFormat();

		DataLine.Info dinfo = new DataLine.Info(SourceDataLine.class, format);
		//format = new AudioFormat(AudioFormat.Encoding.PCM_SIGNED, format.getSampleRate(), format.getSampleSizeInBits(), 2, 2*format.getFrameSize(), format.getFrameRate(), format.isBigEndian());

		try {
			line = (SourceDataLine)AudioSystem.getLine(dinfo);
		} catch (LineUnavailableException e4) {
			return false; 
		}
		dma.buffer = new byte[65536];
//		try {
//			stream.read(dma.buffer);
//		} catch (IOException e3) {
//			// TODO Auto-generated catch block
//			e3.printStackTrace();
//		}
		
		dma.channels = format.getChannels();
		dma.samplebits = format.getSampleSizeInBits();
		dma.samples = dma.buffer.length / format.getFrameSize();
		dma.speed = (int)format.getSampleRate();
		dma.samplepos = 0;
		dma.submission_chunk = 1;
		
		try {
			line.open(format, 4096);
		} catch (LineUnavailableException e5) {
			return false;
		}

		line.start();
		runLine();
		
		snd_inited = true;
		return true;

	}

	static int SNDDMA_GetDMAPos() {		
		dma.samplepos = line.getFramePosition() % dma.samples;
		return dma.samplepos;
	}

	static void SNDDMA_Shutdown() {
		line.stop();
		line.flush();
		line.close();
		line=null;
		snd_inited = false;		
	}

	/*
	==============
	SNDDMA_Submit

	Send sound to device if buffer isn't really the dma buffer
	===============
	*/
	public static void SNDDMA_Submit() {
		runLine();
	}

	void SNDDMA_BeginPainting() {}

	private static int pos = 0;
	static void runLine() {
		
		int p = line.getFramePosition() * format.getFrameSize() % dma.buffer.length;
		System.out.println("run " + p + " " + pos);	
		if (p == 0) {
			writeLine();	
		}
		else if (pos - p < 2048 ) writeLine();		
	}
	
	static void writeLine() {
		line.write(dma.buffer, pos, 4096);
		pos+=4096;
		if (pos>=dma.buffer.length) pos = 0;		
	}

}