summaryrefslogtreecommitdiffstats
path: root/knights/audio.cpp
blob: f1b0d341172cecd25bac0156f33e0b8c51c632f0 (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
169
170
171
172
173
174
175
176
177
/***************************************************************************
                          audio.cpp  -  description
                             -------------------
    begin                : Mon Jan 7 2002
    copyright            : (C) 2003 by Troy Corbin Jr.
    email                : tcorbin@users.sourceforge.net
 ***************************************************************************/

/***************************************************************************
 *                                                                         *
 *   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.                                   *
 *                                                                         *
 ***************************************************************************/

#include "audio.h"
#include <arts/soundserver.h>
#include <arts/dispatcher.h>
#include <arts/flowsystem.h>
#include <arts/connect.h>
#include <ksimpleconfig.h>
#include <kio/netaccess.h>
#include <qptrlist.h>
#include <qfile.h>
#include <qmap.h>

class audioPrivate
{
	public:
		bool														enabled;
		bool														firstTime;
		QString													theme;
		Arts::Dispatcher								dispatcher;
		Arts::SoundServerV2							server;
		Arts::PlayObjectFactory					playObjectFactory;
		QMap<int, Arts::PlayObject>			audioMap;
		QPtrList<KTempFile>							fileRef;
};

audio::audio()
{
	a = new audioPrivate;
	a->server = Arts::Reference("global:Arts_SoundServerV2");
	a->fileRef.setAutoDelete( TRUE );
	if( a->server.isNull() )
	{
		a->enabled = FALSE;
		kdWarning() << "audio::audio: Can not create Arts::SoundServerV2" << endl;
	}
	else
	{
		a->enabled = TRUE;
		a->playObjectFactory = Arts::Reference("global:Arts_PlayObjectFactory");
	}
	a->firstTime = TRUE;
}
audio::~audio()
{
	a->audioMap.clear();
	a->fileRef.clear();
	delete a;
}
void audio::play( const int snd )
{
	if( a->firstTime || !enabled || !a->enabled )
		return;

	QMap<int, Arts::PlayObject>::Iterator IT = a->audioMap.find( snd );

	if( IT == a->audioMap.end() )
	{
		return;
	}

	Arts::PlayObject* current = &IT.data();

	if( volume )
	{
		Arts::Synth_BUS_UPLINK uplink = Arts::DynamicCast( current->_getChild( "uplink" ) );
		uplink.stop();
		current->_node()->stop();
		Arts::disconnect( *current, "left", uplink, "left" );
		Arts::disconnect( *current, "right", uplink, "right" );
		Arts::StereoVolumeControl volumeControl = Arts::DynamicCast( a->server.createObject( "Arts::StereoVolumeControl" ) );
		current->_addChild( volumeControl, "volume" );
		uplink.start();
		volumeControl.start();
		current->_node()->start();
		Arts::connect( *current, "left", volumeControl, "inleft" );
		Arts::connect( *current, "right", volumeControl, "inright" );
		Arts::connect( volumeControl, "outleft", uplink, "left" );
		Arts::connect( volumeControl, "outright", uplink, "right" );
		volumeControl.scaleFactor( ( 100 - volume ) / 100.0 );
	}

	Arts::poTime time = current->currentTime();
	time.seconds = 0;
	time.ms = 0;
	current->seek( time );
	current->play();
}

void audio::setTheme( const QString &newTheme )
{
	QString configFile;
	KSimpleConfig *themeConfig;

	if( !enabled || !a->enabled )
		return;

	a->audioMap.clear();
	a->fileRef.clear();

	if( !KIO::NetAccess::download( newTheme + "theme.conf" , configFile ) )
	{
		kdWarning() << "audio::setTheme: Can not access theme.conf from " << newTheme << endl;
		return;
	}
	themeConfig = new KSimpleConfig( configFile, TRUE );

	/* Read the details about this theme */
	themeConfig->setGroup( "General" );
	themeHeader.name = themeConfig->readEntry( "Name", "Unknown" );
	themeHeader.version = themeConfig->readEntry( "Version", "Unknown" );
	themeHeader.author = themeConfig->readEntry( "Author", "Unknown" );
	themeHeader.authorEmail = themeConfig->readEntry( "AuthorEmail", "Unknown" );
	themeHeader.authorWWW = themeConfig->readEntry( "AuthorWWW", "Unknown" );
	themeHeader.notes = themeConfig->readEntry( "Notes", "Unknown" );

	/* Read the sounds */
	themeConfig->setGroup( "Audio" );
	a->theme = newTheme;
	prepFile( a->theme + themeConfig->readEntry("Select"), SND_SELECT );
	prepFile( a->theme + themeConfig->readEntry("Move"), SND_MOVE );
	prepFile( a->theme + themeConfig->readEntry("Check"), SND_CHECK );
	prepFile( a->theme + themeConfig->readEntry("MatchOver"), SND_MATCH_OVER );
	prepFile( a->theme + themeConfig->readEntry("Challenge"), SND_CHALLENGE );
	prepFile( a->theme + themeConfig->readEntry("Tell"), SND_TELL );
	prepFile( a->theme + themeConfig->readEntry("Notification"), SND_NOTIFICATION );
	prepFile( a->theme + themeConfig->readEntry("DrawOffer"), SND_DRAW_OFFER );
	prepFile( a->theme + themeConfig->readEntry("Say"), SND_SAY );
	prepFile( a->theme + themeConfig->readEntry("Promote"), SND_PROMOTE );
	a->firstTime = FALSE;

	delete themeConfig;
	KIO::NetAccess::removeTempFile( configFile );
}
void audio::prepFile( const QString &source, const int &ref )
{
	Arts::PlayObject player;
	KTempFile *dest;
	QString filename;
	if( source.isEmpty() )
		return;

	dest = new KTempFile( QString::null, source.right(4), 700 );
	dest->setAutoDelete( TRUE );
	filename = dest->name();

	if( !KIO::NetAccess::download( source, filename ) )
	{
		kdWarning() << "audio::prepFile: Can not download " << source << endl;
		delete dest;
		return;
	}
	player = a->playObjectFactory.createPlayObject( QFile::encodeName( dest->name() ).data() );
	if( player.isNull() )
	{
		delete dest;
		return;
	}
	a->fileRef.append( dest );
	a->audioMap[ ref ] = player;
	return;
}