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

Side by Side Diff: third_party/WebKit/Source/modules/webaudio/OscillatorNode.cpp

Issue 2767623002: Slightly reduce memory usage in OscillatorNode (Closed)
Patch Set: Rebase Created 3 years, 8 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
« no previous file with comments | « third_party/WebKit/Source/modules/webaudio/OscillatorNode.h ('k') | no next file » | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 /* 1 /*
2 * Copyright (C) 2012, Google Inc. All rights reserved. 2 * Copyright (C) 2012, Google Inc. All rights reserved.
3 * 3 *
4 * Redistribution and use in source and binary forms, with or without 4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions 5 * modification, are permitted provided that the following conditions
6 * are met: 6 * are met:
7 * 1. Redistributions of source code must retain the above copyright 7 * 1. Redistributions of source code must retain the above copyright
8 * notice, this list of conditions and the following disclaimer. 8 * notice, this list of conditions and the following disclaimer.
9 * 2. Redistributions in binary form must reproduce the above copyright 9 * 2. Redistributions in binary form must reproduce the above copyright
10 * notice, this list of conditions and the following disclaimer in the 10 * notice, this list of conditions and the following disclaimer in the
(...skipping 23 matching lines...) Expand all
34 #include "platform/audio/VectorMath.h" 34 #include "platform/audio/VectorMath.h"
35 #include "platform/wtf/MathExtras.h" 35 #include "platform/wtf/MathExtras.h"
36 #include "platform/wtf/StdLibExtras.h" 36 #include "platform/wtf/StdLibExtras.h"
37 37
38 namespace blink { 38 namespace blink {
39 39
40 using namespace VectorMath; 40 using namespace VectorMath;
41 41
42 OscillatorHandler::OscillatorHandler(AudioNode& node, 42 OscillatorHandler::OscillatorHandler(AudioNode& node,
43 float sample_rate, 43 float sample_rate,
44 const String& oscillator_type,
45 PeriodicWave* wave_table,
44 AudioParamHandler& frequency, 46 AudioParamHandler& frequency,
45 AudioParamHandler& detune) 47 AudioParamHandler& detune)
46 : AudioScheduledSourceHandler(kNodeTypeOscillator, node, sample_rate), 48 : AudioScheduledSourceHandler(kNodeTypeOscillator, node, sample_rate),
47 type_(SINE),
48 frequency_(frequency), 49 frequency_(frequency),
49 detune_(detune), 50 detune_(detune),
50 first_render_(true), 51 first_render_(true),
51 virtual_read_index_(0), 52 virtual_read_index_(0),
52 phase_increments_(AudioUtilities::kRenderQuantumFrames), 53 phase_increments_(AudioUtilities::kRenderQuantumFrames),
53 detune_values_(AudioUtilities::kRenderQuantumFrames) { 54 detune_values_(AudioUtilities::kRenderQuantumFrames) {
54 // Sets up default wavetable. 55 if (wave_table) {
55 SetType(type_); 56 // A PeriodicWave overrides any value for the oscillator type,
57 // forcing the type to be 'custom".
58 SetPeriodicWave(wave_table);
59 } else {
60 if (oscillator_type == "sine")
61 SetType(SINE);
62 else if (oscillator_type == "square")
63 SetType(SQUARE);
64 else if (oscillator_type == "sawtooth")
65 SetType(SAWTOOTH);
66 else if (oscillator_type == "triangle")
67 SetType(TRIANGLE);
68 else
69 NOTREACHED();
70 }
56 71
57 // An oscillator is always mono. 72 // An oscillator is always mono.
58 AddOutput(1); 73 AddOutput(1);
59 74
60 Initialize(); 75 Initialize();
61 } 76 }
62 77
63 PassRefPtr<OscillatorHandler> OscillatorHandler::Create( 78 PassRefPtr<OscillatorHandler> OscillatorHandler::Create(
64 AudioNode& node, 79 AudioNode& node,
65 float sample_rate, 80 float sample_rate,
81 const String& oscillator_type,
82 PeriodicWave* wave_table,
66 AudioParamHandler& frequency, 83 AudioParamHandler& frequency,
67 AudioParamHandler& detune) { 84 AudioParamHandler& detune) {
68 return AdoptRef(new OscillatorHandler(node, sample_rate, frequency, detune)); 85 return AdoptRef(new OscillatorHandler(node, sample_rate, oscillator_type,
86 wave_table, frequency, detune));
69 } 87 }
70 88
71 OscillatorHandler::~OscillatorHandler() { 89 OscillatorHandler::~OscillatorHandler() {
72 Uninitialize(); 90 Uninitialize();
73 } 91 }
74 92
75 String OscillatorHandler::GetType() const { 93 String OscillatorHandler::GetType() const {
76 switch (type_) { 94 switch (type_) {
77 case SINE: 95 case SINE:
78 return "sine"; 96 return "sine";
(...skipping 280 matching lines...) Expand 10 before | Expand all | Expand 10 after
359 periodic_wave_ = periodic_wave; 377 periodic_wave_ = periodic_wave;
360 type_ = CUSTOM; 378 type_ = CUSTOM;
361 } 379 }
362 380
363 bool OscillatorHandler::PropagatesSilence() const { 381 bool OscillatorHandler::PropagatesSilence() const {
364 return !IsPlayingOrScheduled() || HasFinished() || !periodic_wave_.Get(); 382 return !IsPlayingOrScheduled() || HasFinished() || !periodic_wave_.Get();
365 } 383 }
366 384
367 // ---------------------------------------------------------------- 385 // ----------------------------------------------------------------
368 386
369 OscillatorNode::OscillatorNode(BaseAudioContext& context) 387 OscillatorNode::OscillatorNode(BaseAudioContext& context,
388 const String& oscillator_type,
389 PeriodicWave* wave_table)
370 : AudioScheduledSourceNode(context), 390 : AudioScheduledSourceNode(context),
371 // Use musical pitch standard A440 as a default. 391 // Use musical pitch standard A440 as a default.
372 frequency_(AudioParam::Create(context, 392 frequency_(AudioParam::Create(context,
373 kParamTypeOscillatorFrequency, 393 kParamTypeOscillatorFrequency,
374 440, 394 440,
375 -context.sampleRate() / 2, 395 -context.sampleRate() / 2,
376 context.sampleRate() / 2)), 396 context.sampleRate() / 2)),
377 // Default to no detuning. 397 // Default to no detuning.
378 detune_(AudioParam::Create(context, kParamTypeOscillatorDetune, 0)) { 398 detune_(AudioParam::Create(context, kParamTypeOscillatorDetune, 0)) {
379 SetHandler(OscillatorHandler::Create( 399 SetHandler(OscillatorHandler::Create(
380 *this, context.sampleRate(), frequency_->Handler(), detune_->Handler())); 400 *this, context.sampleRate(), oscillator_type, wave_table,
401 frequency_->Handler(), detune_->Handler()));
381 } 402 }
382 403
383 OscillatorNode* OscillatorNode::Create(BaseAudioContext& context, 404 OscillatorNode* OscillatorNode::Create(BaseAudioContext& context,
405 const String& oscillator_type,
406 PeriodicWave* wave_table,
384 ExceptionState& exception_state) { 407 ExceptionState& exception_state) {
385 DCHECK(IsMainThread()); 408 DCHECK(IsMainThread());
386 409
387 if (context.IsContextClosed()) { 410 if (context.IsContextClosed()) {
388 context.ThrowExceptionForClosedState(exception_state); 411 context.ThrowExceptionForClosedState(exception_state);
389 return nullptr; 412 return nullptr;
390 } 413 }
391 414
392 return new OscillatorNode(context); 415 return new OscillatorNode(context, oscillator_type, wave_table);
393 } 416 }
394 417
395 OscillatorNode* OscillatorNode::Create(BaseAudioContext* context, 418 OscillatorNode* OscillatorNode::Create(BaseAudioContext* context,
396 const OscillatorOptions& options, 419 const OscillatorOptions& options,
397 ExceptionState& exception_state) { 420 ExceptionState& exception_state) {
398 OscillatorNode* node = Create(*context, exception_state); 421 if (options.type() == "custom" && !options.hasPeriodicWave()) {
422 exception_state.ThrowDOMException(
423 kInvalidStateError,
424 "A PeriodicWave must be specified if the type is set to \"custom\"");
425 return nullptr;
426 }
427
428 OscillatorNode* node =
429 Create(*context, options.type(), options.periodicWave(), exception_state);
399 430
400 if (!node) 431 if (!node)
401 return nullptr; 432 return nullptr;
402 433
403 node->HandleChannelOptions(options, exception_state); 434 node->HandleChannelOptions(options, exception_state);
404 435
405 if (options.hasPeriodicWave()) {
406 // Set up the custom wave; this also sets the type to "custom",
407 // overriding any |type| option the user may have set. Per spec.
408 node->setPeriodicWave(options.periodicWave());
409 } else {
410 node->setType(options.type(), exception_state);
411 }
412
413 node->detune()->setValue(options.detune()); 436 node->detune()->setValue(options.detune());
414 node->frequency()->setValue(options.frequency()); 437 node->frequency()->setValue(options.frequency());
415 438
416 return node; 439 return node;
417 } 440 }
418 441
419 DEFINE_TRACE(OscillatorNode) { 442 DEFINE_TRACE(OscillatorNode) {
420 visitor->Trace(frequency_); 443 visitor->Trace(frequency_);
421 visitor->Trace(detune_); 444 visitor->Trace(detune_);
422 AudioScheduledSourceNode::Trace(visitor); 445 AudioScheduledSourceNode::Trace(visitor);
(...skipping 18 matching lines...) Expand all
441 464
442 AudioParam* OscillatorNode::detune() { 465 AudioParam* OscillatorNode::detune() {
443 return detune_; 466 return detune_;
444 } 467 }
445 468
446 void OscillatorNode::setPeriodicWave(PeriodicWave* wave) { 469 void OscillatorNode::setPeriodicWave(PeriodicWave* wave) {
447 GetOscillatorHandler().SetPeriodicWave(wave); 470 GetOscillatorHandler().SetPeriodicWave(wave);
448 } 471 }
449 472
450 } // namespace blink 473 } // namespace blink
OLDNEW
« no previous file with comments | « third_party/WebKit/Source/modules/webaudio/OscillatorNode.h ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698