Tizen 2.0 Release
[framework/osp/media.git] / src / FMedia_ToneGenerator.cpp
1 //
2 // Open Service Platform
3 // Copyright (c) 2012 Samsung Electronics Co., Ltd.
4 //
5 // Licensed under the Apache License, Version 2.0 (the License);
6 // you may not use this file except in compliance with the License.
7 // You may obtain a copy of the License at
8 //
9 //     http://www.apache.org/licenses/LICENSE-2.0
10 //
11 // Unless required by applicable law or agreed to in writing, software
12 // distributed under the License is distributed on an "AS IS" BASIS,
13 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 // See the License for the specific language governing permissions and
15 // limitations under the License.
16 //
17
18 #include <math.h>
19 #include <FMediaToneTypes.h>
20 #include <FBaseSysLog.h>
21 #include "FMedia_ToneGenerator.h"
22
23 using namespace Tizen::Base;
24 namespace Tizen {namespace Media
25 {
26
27 _ToneGenerator::_ToneGenerator(void)
28 {
29 }
30
31 _ToneGenerator::~_ToneGenerator(void)
32 {
33 }
34
35 bool
36 _ToneGenerator::GenerateTone(Tizen::Base::ByteBuffer& toneBuffer, const int& firstFrequency, const int& secondFrequency,
37                                                          long startSample, long endSample,
38                                                          int volume)
39 {
40         result r = E_SUCCESS;
41         short tempTone = 0;
42         float first = 2 * M_PI * firstFrequency / (float) SAMPLING_RATE;
43         float second = 2 * M_PI * secondFrequency / (float) SAMPLING_RATE;
44         int j = toneBuffer.GetPosition();
45
46         //Gives the number of samples
47         j = j / _BYTES_PER_SAMPLE;
48
49         // Generate tone for the given samples
50         for (long i = startSample; i < endSample; i++, j++)
51         {
52                 tempTone = ((sin(first * i) + sin(second * i)) * ((_MAX_TONE_PLAYER_VOLUME_VALUE * volume) / 100));
53                 r = toneBuffer.SetShort(_BYTES_PER_SAMPLE * j, tempTone);
54                 SysTryReturn(NID_MEDIA, r == E_SUCCESS , false, false, "[%s] Propagating", GetErrorMessage(r));
55
56         }
57
58         r = toneBuffer.SetPosition((j) * _BYTES_PER_SAMPLE);
59         SysTryReturn(NID_MEDIA, r == E_SUCCESS , false, false, "[%s] Propagating", GetErrorMessage(r));
60
61         return true;
62 }
63
64 };
65 };