summaryrefslogtreecommitdiffstats
path: root/noatun-plugins/pitchablespeed/pitchablespeed.cpp
blob: 5ebaf27493d01dfc382e1a2870b36ba56dc0a1ff (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
/***************************************************************************
 *                                                                         *
 *   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.                                   *
 *                                                                         *
 ***************************************************************************/

// (c) 2001 hans_meine@gmx.net

#include "pitchablespeed.h"
#include <arts/reference.h>
#include <arts/soundserver.h>
#include <arts/kmedia2.h>
#include <noatun/app.h>
#include <noatun/player.h>
#include <noatun/engine.h>
#include <noatun/noatunarts.h>
#include <tqslider.h>
#include <tqlineedit.h>
#include <tqcheckbox.h>
#include <tdelocale.h>
#include <knuminput.h>

extern "C"
{
	Plugin *create_plugin()
	{
		TDEGlobal::locale()->insertCatalogue("pitchablespeed");
		return new PitchableSpeed();
	}
}

const int PitchableSpeed::DEFAULT_OFFSET = 5;
const int PitchableSpeed::DEFAULT_RANGE = 10;

PitchableSpeed::PitchableSpeed() : Plugin(),
	offset(DEFAULT_OFFSET),
	range(DEFAULT_RANGE)
,	frequenciesPreserved(false)
{
	
	rangeInput->setRange(2,150,1,false);
	rangeInput->setValue(range);
	offsetInput->setRange(-5,15,1,false);
	offsetInput->setValue(offset);
	setupSliderRange();
	connect(napp->player(), TQT_SIGNAL(changed()),
			this, TQT_SLOT(newSong()));
	connect(preserveFrequencies, TQT_SIGNAL(toggled(bool)),
			this, TQT_SLOT(preserveFrequenciesChanged(bool)));
	//show();
}

PitchableSpeed::~PitchableSpeed()
{
	napp->pluginMenuRemove(pluginMenuItem);

	Arts::PlayObject playobject= napp->player()->engine()->playObject();
	Arts::PitchablePlayObject pitchable= Arts::DynamicCast(playobject);
	if (!pitchable.isNull())
		pitchable.speed(1.0);

	if (frequenciesPreserved)
		napp->player()->engine()->effectStack()->remove(pitchShiftEffectID);
}

void PitchableSpeed::newSong()
{
	setSpeed();

	Arts::PlayObject playobject= napp->player()->engine()->playObject();
	Arts::PitchablePlayObject pitchable= Arts::DynamicCast(playobject);
	slider->setEnabled(!pitchable.isNull());
}

float PitchableSpeed::sliderSpeed()
{
	return 1.0f - slider->value()/1000.0f;
}

void PitchableSpeed::setSpeed()
{
	Arts::PlayObject playobject= napp->player()->engine()->playObject();
	Arts::PitchablePlayObject pitchable= Arts::DynamicCast(playobject);
	float newSpeed= 1.0f;
	if (!pitchable.isNull())
	{
		newSpeed= sliderSpeed();
		pitchable.speed(newSpeed);
		if (frequenciesPreserved)
			pitchShift.speed(1.f/newSpeed);
	}

	TQString percentStr;
	percentStr.setNum((newSpeed-1.f)*100.f,'f',1);
	if (newSpeed<=0) percentStr.prepend('+').append(' ').append('%');
	currentValEdit->setText(percentStr);
}

void PitchableSpeed::setupSliderRange()
{
	int min= (-offset+range)<99 ? (-offset+range)*10 : 990;
	slider->setRange((-offset-range)*10,min);
	slider->setTickInterval(10);
}

void PitchableSpeed::speedChanged(int )
{
	setSpeed();
}

void PitchableSpeed::offsetChanged(int newOffset)
{
	offset= newOffset;
	setupSliderRange();
}

void PitchableSpeed::rangeChanged(int newRange)
{
	range= newRange;
	setupSliderRange();
}

void PitchableSpeed::preserveFrequenciesChanged(bool preserveNow)
{
	if (preserveNow==frequenciesPreserved)
		return;
	if (preserveNow)
	{
		pitchShift = Arts::DynamicCast(napp->player()->engine()->server()->
				 createObject("Arts::Synth_STEREO_PITCH_SHIFT"));
		pitchShift.frequency(10.f); // TODO: might have to experiment a bit with this
		pitchShift.speed(1.f/sliderSpeed());
		pitchShift.start();
		pitchShiftEffectID= napp->player()->engine()->effectStack()->
							insertTop(pitchShift, "correcting pitch shift");
	}
	else
	{
		napp->player()->engine()->effectStack()->remove(pitchShiftEffectID);
		pitchShift.stop();
		pitchShift = Arts::Synth_STEREO_PITCH_SHIFT::null();
	}
	frequenciesPreserved= preserveNow;
}

void PitchableSpeed::init()
{
	pluginMenuItem = napp->pluginMenuAdd(i18n("Toggle Speed Slider"),
										 TQT_TQOBJECT(this), TQT_SLOT(toggle(void)));
}

void PitchableSpeed::toggle(void)
{
	show();
	raise();
}

void PitchableSpeed::closeEvent(TQCloseEvent *)
{
	hide();
}

#include "pitchablespeed.moc"