Andreas Huber | da050cf2 | 2009-09-02 14:01:43 -0700 | [diff] [blame] | 1 | #include "SineSource.h" |
| 2 | |
| 3 | #include <math.h> |
| 4 | |
| 5 | #include <media/stagefright/MediaBufferGroup.h> |
| 6 | #include <media/stagefright/MediaDebug.h> |
Andreas Huber | e6c4096 | 2009-09-10 14:13:30 -0700 | [diff] [blame] | 7 | #include <media/stagefright/MediaDefs.h> |
Andreas Huber | da050cf2 | 2009-09-02 14:01:43 -0700 | [diff] [blame] | 8 | #include <media/stagefright/MetaData.h> |
| 9 | |
| 10 | namespace android { |
| 11 | |
| 12 | SineSource::SineSource(int32_t sampleRate, int32_t numChannels) |
| 13 | : mStarted(false), |
| 14 | mSampleRate(sampleRate), |
| 15 | mNumChannels(numChannels), |
| 16 | mPhase(0), |
| 17 | mGroup(NULL) { |
| 18 | CHECK(numChannels == 1 || numChannels == 2); |
| 19 | } |
| 20 | |
| 21 | SineSource::~SineSource() { |
| 22 | if (mStarted) { |
| 23 | stop(); |
| 24 | } |
| 25 | } |
| 26 | |
| 27 | status_t SineSource::start(MetaData *params) { |
| 28 | CHECK(!mStarted); |
| 29 | |
| 30 | mGroup = new MediaBufferGroup; |
| 31 | mGroup->add_buffer(new MediaBuffer(kBufferSize)); |
| 32 | |
| 33 | mPhase = 0; |
| 34 | mStarted = true; |
| 35 | |
| 36 | return OK; |
| 37 | } |
| 38 | |
| 39 | status_t SineSource::stop() { |
| 40 | CHECK(mStarted); |
| 41 | |
| 42 | delete mGroup; |
| 43 | mGroup = NULL; |
| 44 | |
| 45 | mStarted = false; |
| 46 | |
| 47 | return OK; |
| 48 | } |
| 49 | |
| 50 | sp<MetaData> SineSource::getFormat() { |
| 51 | sp<MetaData> meta = new MetaData; |
Andreas Huber | e6c4096 | 2009-09-10 14:13:30 -0700 | [diff] [blame] | 52 | meta->setCString(kKeyMIMEType, MEDIA_MIMETYPE_AUDIO_RAW); |
Andreas Huber | da050cf2 | 2009-09-02 14:01:43 -0700 | [diff] [blame] | 53 | meta->setInt32(kKeyChannelCount, mNumChannels); |
| 54 | meta->setInt32(kKeySampleRate, mSampleRate); |
Andreas Huber | 8768f2c | 2009-12-01 15:26:54 -0800 | [diff] [blame] | 55 | meta->setInt32(kKeyMaxInputSize, kBufferSize); |
Andreas Huber | da050cf2 | 2009-09-02 14:01:43 -0700 | [diff] [blame] | 56 | |
| 57 | return meta; |
| 58 | } |
| 59 | |
| 60 | status_t SineSource::read( |
| 61 | MediaBuffer **out, const ReadOptions *options) { |
| 62 | *out = NULL; |
| 63 | |
| 64 | MediaBuffer *buffer; |
| 65 | status_t err = mGroup->acquire_buffer(&buffer); |
| 66 | |
| 67 | if (err != OK) { |
| 68 | return err; |
| 69 | } |
| 70 | |
| 71 | size_t frameSize = mNumChannels * sizeof(int16_t); |
| 72 | size_t numFramesPerBuffer = buffer->size() / frameSize; |
| 73 | |
| 74 | int16_t *ptr = (int16_t *)buffer->data(); |
| 75 | |
| 76 | const double k = kFrequency / mSampleRate * (2.0 * M_PI); |
| 77 | |
| 78 | double x = mPhase * k; |
| 79 | for (size_t i = 0; i < numFramesPerBuffer; ++i) { |
| 80 | int16_t amplitude = (int16_t)(32767.0 * sin(x)); |
| 81 | |
| 82 | *ptr++ = amplitude; |
| 83 | if (mNumChannels == 2) { |
| 84 | *ptr++ = amplitude; |
| 85 | } |
| 86 | |
| 87 | x += k; |
| 88 | } |
| 89 | |
Andreas Huber | fa8de75 | 2009-10-08 10:07:49 -0700 | [diff] [blame] | 90 | buffer->meta_data()->setInt64( |
| 91 | kKeyTime, ((int64_t)mPhase * 1000000) / mSampleRate); |
Andreas Huber | da050cf2 | 2009-09-02 14:01:43 -0700 | [diff] [blame] | 92 | |
| 93 | mPhase += numFramesPerBuffer; |
| 94 | |
| 95 | buffer->set_range(0, numFramesPerBuffer * frameSize); |
| 96 | |
| 97 | *out = buffer; |
| 98 | |
| 99 | return OK; |
| 100 | } |
| 101 | |
| 102 | } // namespace android |