diff options
author | brettw@chromium.org <brettw@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98> | 2010-11-01 16:16:50 +0000 |
---|---|---|
committer | brettw@chromium.org <brettw@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98> | 2010-11-01 16:16:50 +0000 |
commit | 1758e88fd909ea0ffd49621e8066ffad5627ffdf (patch) | |
tree | c304a5eed047cae5665f5af1739d84655fb5815d /ppapi/examples/audio | |
parent | e7d8b51953b7d3b2b8a0aba46132305b32f3efce (diff) | |
download | chromium_src-1758e88fd909ea0ffd49621e8066ffad5627ffdf.zip chromium_src-1758e88fd909ea0ffd49621e8066ffad5627ffdf.tar.gz chromium_src-1758e88fd909ea0ffd49621e8066ffad5627ffdf.tar.bz2 |
Move PPAPI into the Chrome repo. The old repo was
http://ppapi.googlecode.com/
TEST=none
BUG=none
git-svn-id: svn://svn.chromium.org/chrome/trunk/src@64613 0039d316-1c4b-4281-b951-d872f2087c98
Diffstat (limited to 'ppapi/examples/audio')
-rw-r--r-- | ppapi/examples/audio/audio.cc | 80 |
1 files changed, 80 insertions, 0 deletions
diff --git a/ppapi/examples/audio/audio.cc b/ppapi/examples/audio/audio.cc new file mode 100644 index 0000000..8f80ba4 --- /dev/null +++ b/ppapi/examples/audio/audio.cc @@ -0,0 +1,80 @@ +// Copyright (c) 2010 The Chromium Authors. All rights reserved. +// Use of this source code is governed by a BSD-style license that can be +// found in the LICENSE file. + +#include <cmath> +#include <limits> + +#include "ppapi/cpp/dev/audio_dev.h" +#include "ppapi/cpp/instance.h" +#include "ppapi/cpp/module.h" + +// Separate left and right frequency to make sure we didn't swap L & R. +// Sounds pretty horrible, though... +const double frequency_l = 200; +const double frequency_r = 1000; + +// This sample frequency is guaranteed to work. +const PP_AudioSampleRate_Dev sample_frequency = PP_AUDIOSAMPLERATE_44100; +const uint32_t sample_count = 4096; + +class MyInstance : public pp::Instance { + public: + explicit MyInstance(PP_Instance instance) + : pp::Instance(instance), + audio_time_(0) { + } + + virtual bool Init(uint32_t argc, const char* argn[], const char* argv[]) { + audio_ = pp::Audio_Dev( + *this, pp::AudioConfig_Dev(sample_frequency, sample_count), + SineWaveCallback, this); + return audio_.StartPlayback(); + } + + private: + static void SineWaveCallback(void* samples, + size_t buffer_size_in_bytes, + void* thiz) { + const double th_l = 2 * 3.141592653589 * frequency_l / sample_frequency; + const double th_r = 2 * 3.141592653589 * frequency_r / sample_frequency; + + // Store time value to avoid clicks on buffer boundries. + size_t t = reinterpret_cast<MyInstance*>(thiz)->audio_time_; + + uint16_t* buf = reinterpret_cast<uint16_t*>(samples); + for (size_t buffer_index = 0u; + buffer_index < buffer_size_in_bytes; + buffer_index += 2) { + *buf++ = static_cast<uint16_t>(std::sin(th_l * t) + * std::numeric_limits<uint16_t>::max()); + *buf++ = static_cast<uint16_t>(std::sin(th_r * t++) + * std::numeric_limits<uint16_t>::max()); + } + reinterpret_cast<MyInstance*>(thiz)->audio_time_ = t; + } + + // Audio resource. Allocated in Init(), freed on destruction. + pp::Audio_Dev audio_; + + // Audio buffer time. Used to make prevent sine wave skips on buffer + // boundaries. + size_t audio_time_; +}; + +class MyModule : public pp::Module { + public: + // Override CreateInstance to create your customized Instance object. + virtual pp::Instance* CreateInstance(PP_Instance instance) { + return new MyInstance(instance); + } +}; + +namespace pp { + +// Factory function for your specialization of the Module object. +Module* CreateModule() { + return new MyModule(); +} + +} // namespace pp |