Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(179)

Side by Side Diff: ppapi/native_client/tests/ppapi_simple_tests/audio.cc

Issue 7740013: Cloning a bunch of stuff from the native_client repository at r6528 (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src/
Patch Set: Created 9 years, 3 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
OLDNEW
(Empty)
1 // Copyright (c) 2011 The Native Client Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
4 //
5
6 #include <stdint.h>
7
8 #include <cmath>
9 #include <limits>
10 #include <string>
11
12 #include <nacl/nacl_check.h>
13 #include <nacl/nacl_log.h>
14
15 #include "ppapi/c/pp_bool.h"
16 #include "ppapi/c/pp_errors.h"
17 #include "ppapi/c/ppb_audio.h"
18 #include "ppapi/c/ppb_audio_config.h"
19 #include "ppapi/cpp/audio.h"
20 #include "ppapi/cpp/audio_config.h"
21 #include "ppapi/cpp/completion_callback.h"
22 #include "ppapi/cpp/instance.h"
23 #include "ppapi/cpp/module.h"
24
25 const PP_AudioSampleRate kSampleFrequency = PP_AUDIOSAMPLERATE_44100;
26 // Buffer size in units of sample frames.
27 // 4096 is a conservative size that should avoid underruns on most systems.
28 const uint32_t kSampleFrameCount = 4096;
29 const uint32_t kDefaultFrequency = 400;
30 const uint32_t kDefaultDuration = 10000;
31
32 const int kNumChannelsForStereo = 2;
33 const size_t kSizeSingleSample = kNumChannelsForStereo * sizeof(int16_t);
34
35 const double kPi = 3.141592653589;
36 const double kTwoPi = 2.0 * kPi;
37
38 class MyInstance : public pp::Instance {
39 private:
40 pp::Audio audio_;
41 uint32_t obtained_sample_frame_count_;
42 double audio_wave_time_; // between -kTwoPi, +kTwoPi
43 uint32_t duration_; // in msec
44 uint32_t frequency_;
45
46 static void SoundCallback(void* samples, uint32_t num_bytes, void* thiz) {
47 MyInstance* instance = reinterpret_cast<MyInstance*>(thiz);
48 // CHECK inside callback is only for testing purposes.
49 CHECK(instance->obtained_sample_frame_count_ * kSizeSingleSample ==
50 num_bytes);
51 const double delta = kTwoPi * instance->frequency_ / kSampleFrequency;
52 const int16_t max_int16 = std::numeric_limits<int16_t>::max();
53 int16_t* buf = reinterpret_cast<int16_t*>(samples);
54 for (size_t i = 0; i < instance->obtained_sample_frame_count_; ++i) {
55 const double v = sin(instance->audio_wave_time_) * max_int16;
56 *buf++ = static_cast<int16_t>(v);
57 *buf++ = static_cast<int16_t>(v);
58 // Add delta, keep within -kTwoPi, +TwoPi to preserve precision.
59 instance->audio_wave_time_ += delta;
60 if (instance->audio_wave_time_ > kTwoPi)
61 instance->audio_wave_time_ -= kTwoPi * 2.0;
62 }
63 }
64
65 static void StopOutput(void* thiz, int32_t err) {
66 if (PP_OK == err) {
67 MyInstance* instance = static_cast<MyInstance*>(thiz);
68 instance->audio_.StopPlayback();
69 }
70 }
71
72 void ExtraChecks(pp::AudioConfig* config) {
73 CHECK(obtained_sample_frame_count_ >= PP_AUDIOMINSAMPLEFRAMECOUNT);
74 CHECK(obtained_sample_frame_count_ <= PP_AUDIOMAXSAMPLEFRAMECOUNT);
75
76 PPB_GetInterface get_browser_if =
77 pp::Module::Get()->get_browser_interface();
78
79 const struct PPB_AudioConfig* audio_config_if =
80 static_cast<const struct PPB_AudioConfig*>(
81 get_browser_if(PPB_AUDIO_CONFIG_INTERFACE));
82
83 const struct PPB_Audio* audio_if =
84 static_cast<const struct PPB_Audio*>(
85 get_browser_if(PPB_AUDIO_INTERFACE));
86
87 CHECK(NULL != audio_config_if);
88 CHECK(NULL != audio_if);
89
90 const PP_Resource audio_config_res = config->pp_resource();
91 const PP_Resource audio_res = audio_.pp_resource();
92
93 CHECK(PP_TRUE == audio_config_if->IsAudioConfig(audio_config_res));
94 CHECK(PP_TRUE == audio_if->IsAudio(audio_res));
95 CHECK(PP_FALSE == audio_config_if->IsAudioConfig(audio_res));
96 CHECK(PP_FALSE == audio_if->IsAudio(audio_config_res));
97 CHECK(audio_if->GetCurrentConfig(audio_res) == audio_config_res);
98 CHECK(0 == audio_if->GetCurrentConfig(audio_config_res));
99 CHECK(audio_config_if->GetSampleRate(audio_config_res) ==
100 config->sample_rate());
101 CHECK(audio_config_if->GetSampleFrameCount(audio_config_res) ==
102 config->sample_frame_count());
103 CHECK(audio_.config().pp_resource() == audio_config_res);
104 }
105
106 void ParseArgs(uint32_t argc, const char* argn[], const char* argv[]) {
107 for (uint32_t i = 0; i < argc; ++i) {
108 const std::string tag = argn[i];
109 if (tag == "duration") duration_ = strtol(argv[i], 0, 0);
110 if (tag == "frequency") frequency_ = strtol(argv[i], 0, 0);
111 }
112 }
113
114 public:
115 explicit MyInstance(PP_Instance instance)
116 : pp::Instance(instance),
117 duration_(kDefaultDuration), frequency_(kDefaultFrequency) {}
118
119 virtual ~MyInstance() {}
120
121 virtual bool Init(uint32_t argc, const char* argn[], const char* argv[]) {
122 ParseArgs(argc, argn, argv);
123 obtained_sample_frame_count_ = pp::AudioConfig::RecommendSampleFrameCount(
124 kSampleFrequency, kSampleFrameCount);
125
126 pp::AudioConfig config =
127 pp::AudioConfig(this, kSampleFrequency, obtained_sample_frame_count_);
128
129 audio_ = pp::Audio(this, config, SoundCallback, this);
130
131 ExtraChecks(&config);
132
133 CHECK(audio_.StartPlayback());
134
135 pp::CompletionCallback cc(StopOutput, this);
136 pp::Module::Get()->core()->CallOnMainThread(duration_, cc, PP_OK);
137 return true;
138 }
139 };
140
141 // standard boilerplate code below
142 class MyModule : public pp::Module {
143 public:
144 virtual pp::Instance* CreateInstance(PP_Instance instance) {
145 return new MyInstance(instance);
146 }
147 };
148
149 namespace pp {
150 Module* CreateModule() {
151 return new MyModule();
152 }
153
154 } // namespace pp
OLDNEW
« no previous file with comments | « ppapi/native_client/tests/ppapi_messaging/ppapi_messaging.r ('k') | ppapi/native_client/tests/ppapi_simple_tests/audio.stdin » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698