1 /*******************************************************************************
2  * Copyright 2009-2016 Jörg Müller
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *   http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  ******************************************************************************/
16 
17 #pragma once
18 
19 /**
20  * @file SquareReader.h
21  * @ingroup generator
22  * The SquareReader class.
23  */
24 
25 #include "IReader.h"
26 
27 AUD_NAMESPACE_BEGIN
28 
29 /**
30  * This class is used for square tone playback.
31  * The output format is in the 16 bit format and stereo, the sample rate can be
32  * specified.
33  * As the two channels both play the same the output could also be mono, but
34  * in most cases this will result in having to resample for output, so stereo
35  * sound is created directly.
36  */
37 class AUD_API SquareReader : public IReader
38 {
39 private:
40 	/**
41 	 * The frequency of the sine wave.
42 	 */
43 	float m_frequency;
44 
45 	/**
46 	 * The current position in samples.
47 	 */
48 	int m_position;
49 
50 	/**
51 	 * The value of the current sample.
52 	 */
53 	float m_sample;
54 
55 	/**
56 	 * The sample rate for the output.
57 	 */
58 	const SampleRate m_sampleRate;
59 
60 	// delete copy constructor and operator=
61 	SquareReader(const SquareReader&) = delete;
62 	SquareReader& operator=(const SquareReader&) = delete;
63 
64 public:
65 	/**
66 	 * Creates a new reader.
67 	 * \param frequency The frequency of the sine wave.
68 	 * \param sampleRate The output sample rate.
69 	 */
70 	SquareReader(float frequency, SampleRate sampleRate);
71 
72 	/**
73 	 * Sets the frequency of the wave.
74 	 * @param frequency The new frequency in Hertz.
75 	 */
76 	void setFrequency(float frequency);
77 
78 	virtual bool isSeekable() const;
79 	virtual void seek(int position);
80 	virtual int getLength() const;
81 	virtual int getPosition() const;
82 	virtual Specs getSpecs() const;
83 	virtual void read(int & length, bool &eos, sample_t* buffer);
84 };
85 
86 AUD_NAMESPACE_END
87