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

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

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