1 /*
2  * Copyright (C) 2002 - David W. Durham
3  *
4  * This file is part of ReZound, an audio editing application.
5  *
6  * ReZound is free software; you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License as published
8  * by the Free Software Foundation; either version 2 of the License,
9  * or (at your option) any later version.
10  *
11  * ReZound is distributed in the hope that it will be useful, but
12  * WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14  * GNU General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public License
17  * along with this program; if not, write to the Free Software
18  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA
19  */
20 
21 #include "CActionSound.h"
22 
23 #include <stdexcept>
24 
25 #include "CSoundPlayerChannel.h"
26 #include "CSound.h"
27 
28 #include "settings.h"
29 #include "unit_conv.h"
30 
31 
32 // ----------------------------------------------------------------------
33 // -- CActionSound ------------------------------------------------------
34 // ----------------------------------------------------------------------
35 
CActionSound(CSoundPlayerChannel * _channel,CrossfadeEdgesTypes _doCrossfadeEdges)36 CActionSound::CActionSound(CSoundPlayerChannel *_channel,CrossfadeEdgesTypes _doCrossfadeEdges) :
37 	sound(_channel->getSound()),
38 	doCrossfadeEdges(_doCrossfadeEdges)
39 {
40 	if(_channel==NULL)
41 		throw(runtime_error(string(__func__)+" -- channel parameter is NULL"));
42 
43 	start=_channel->getStartPosition();
44 	stop=_channel->getStopPosition();
45 
46 	playPosition=_channel->getPosition();
47 
48 	// now shift playPosition by a setting
49 	const int playPositionShift=s_to_samples_offset(gPlayPositionShift,sound->getSampleRate());
50 	if(playPositionShift!=0)
51 	{
52 		if(playPositionShift>0 && (playPosition+playPositionShift)<sound->getLength()) // don't overrun the length
53 			playPosition+=playPositionShift;
54 		else if(playPositionShift<0 && (sample_fpos_t)playPosition>playPositionShift) // don't underrun zero
55 			playPosition+=playPositionShift;
56 		else
57 			playPosition=0;
58 	}
59 
60 	allChannels();
61 }
62 
CActionSound(const CActionSound & src)63 CActionSound::CActionSound(const CActionSound &src)
64 {
65 	operator=(src);
66 }
67 
countChannels() const68 unsigned CActionSound::countChannels() const
69 {
70 	unsigned i=0;
71 	for(unsigned t=0;t<MAX_CHANNELS;t++)
72 	{
73 		if(doChannel[t])
74 			i++;
75 	}
76 	return i;
77 }
78 
allChannels()79 void CActionSound::allChannels()
80 {
81 	for(unsigned t=0;t<sound->getChannelCount();t++)
82 		doChannel[t]=true;
83 	for(unsigned t=sound->getChannelCount();t<MAX_CHANNELS;t++)
84 		doChannel[t]=false;
85 }
86 
noChannels()87 void CActionSound::noChannels()
88 {
89 	for(unsigned t=0;t<MAX_CHANNELS;t++)
90 		doChannel[t]=false;
91 }
92 
selectionLength() const93 sample_pos_t CActionSound::selectionLength() const
94 {
95 	return (stop-start)+1;
96 }
97 
selectAll() const98 void CActionSound::selectAll() const
99 {
100 	start=0;
101 	stop=sound->getLength()-1;
102 }
103 
selectNone() const104 void CActionSound::selectNone() const
105 {
106 	start=1;
107 	stop=0;
108 }
109 
operator =(const CActionSound & rhs)110 CActionSound &CActionSound::operator=(const CActionSound &rhs)
111 {
112 	sound=rhs.sound;
113 	doCrossfadeEdges=rhs.doCrossfadeEdges;
114 
115 	for(unsigned t=0;t<MAX_CHANNELS;t++)
116 		doChannel[t]=rhs.doChannel[t];
117 
118 	start=rhs.start;
119 	stop=rhs.stop;
120 	playPosition=rhs.playPosition;
121 
122 	return *this;
123 }
124 
125 
126 
127