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

Side by Side Diff: chrome/browser/ui/tabs/tab_audio_indicator.cc

Issue 12744003: Audio indicator: cross platform drawing and animation (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: address review comments Created 7 years, 9 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) 2013 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 #include "chrome/browser/ui/tabs/tab_audio_indicator.h"
6
7 #include "grit/theme_resources.h"
8 #include "ui/base/animation/linear_animation.h"
9 #include "ui/base/resource/resource_bundle.h"
10 #include "ui/gfx/canvas.h"
11 #include "ui/gfx/rect.h"
12
13 namespace {
14
15 // The number of columns to draw for the equalizer graphic.
16 const size_t kEqualizerColumnCount = 3;
17
18 // The maximum level for the equalizer.
19 const size_t kEqualizerMaxLevel = 8;
20
21 // The equalizer cycles between these frames. An equalizer frame is 3 columns
22 // where each column ranges from 0 to |kEqualizerMaxLevel|. TODO(sail): Replace
23 // this with levels from the actual audio source.
24 const size_t kEqualizerFrames[][kEqualizerColumnCount] = {
25 { 7, 5, 3 },
26 { 4, 5, 7 },
27 { 4, 2, 5 },
28 { 3, 7, 4 },
29 { 2, 3, 2 },
30 { 3, 4, 3 },
31 };
32
33 // The duration of each equalizer frame.
34 const size_t kAnimationCycleDurationMs = 300;
35
36 // The duration of the "ending" animation once audio stops playing.
37 const size_t kAnimationEndingDurationMs = 1000;
38
39 // Target frames per second. In reality fewer frames are drawn because the
40 // equalizer levels change slowly.
41 const int kFPS = 15;
42
43 } // namespace
44
45 TabAudioIndicator::TabAudioIndicator(Delegate* delegate)
46 : delegate_(delegate),
47 frame_index_(0),
48 state_(STATE_NOT_ANIMATING) {
49 }
50
51 TabAudioIndicator::~TabAudioIndicator() {
52 }
53
54 void TabAudioIndicator::SetIsPlayingAudio(bool is_playing_audio) {
55 if (is_playing_audio && state_ != STATE_ANIMATING) {
56 state_ = STATE_ANIMATING;
57 animation_.reset(
58 new ui::LinearAnimation(kAnimationCycleDurationMs, kFPS, this));
59 animation_->Start();
60 } else if (!is_playing_audio && state_ == STATE_ANIMATING) {
61 state_ = STATE_ANIMATION_ENDING;
62 animation_.reset(
63 new ui::LinearAnimation(kAnimationEndingDurationMs, kFPS, this));
64 animation_->Start();
65 }
66 }
67
68 bool TabAudioIndicator::IsAnimating() {
69 return state_ != STATE_NOT_ANIMATING;
70 }
71
72 void TabAudioIndicator::Paint(gfx::Canvas* canvas, const gfx::Rect& rect) {
73 if (state_ == STATE_NOT_ANIMATING)
74 return;
75
76 canvas->Save();
77 canvas->ClipRect(rect);
78
79 // Draw 3 equalizer columns. |IDR_AUDIO_EQUALIZER_COLUMN| is a column of the
80 // equalizer with 8 levels. The current level is between 0 and 8 so the
81 // image is shifted down and then drawn.
82 ui::ResourceBundle& rb = ResourceBundle::GetSharedInstance();
83 gfx::ImageSkia* image(rb.GetImageSkiaNamed(IDR_AUDIO_EQUALIZER_COLUMN));
84 int x = rect.x();
85 std::vector<int> levels = GetCurrentEqualizerLevels();
86 for (size_t i = 0; i < levels.size(); ++i) {
87 // Shift the image down by the level. For example, for level 8 draw the
88 // image at rect.y(), For level 7, draw the image at rect.y() - 2, etc...
89 int y = rect.y() + (kEqualizerMaxLevel - levels[i]) * 2;
90 canvas->DrawImageInt(*image, x, y);
91 x += image->width() - 1;
92 }
93
94 canvas->Restore();
95
96 // Cache the levels that were just drawn. This is used to prevent unnecessary
97 // drawing when animation progress doesn't result in equalizer levels
98 // changing.
99 last_displayed_equalizer_levels_ = levels;
100 }
101
102 void TabAudioIndicator::AnimationProgressed(const ui::Animation* animation) {
103 std::vector<int> levels = GetCurrentEqualizerLevels();
104 if (last_displayed_equalizer_levels_ != levels)
105 delegate_->ScheduleAudioIndicatorPaint();
106 }
107
108 void TabAudioIndicator::AnimationEnded(const ui::Animation* animation) {
109 if (state_ == STATE_ANIMATING) {
110 // The current equalizer frame animation has finished. Start animating the
111 // next frame.
112 frame_index_ = (frame_index_ + 1) % arraysize(kEqualizerFrames);
113 animation_->Start();
114 } else if (state_ == STATE_ANIMATION_ENDING) {
115 // The "ending" animation has stopped. Update the tab state so that the UI
116 // can update the tab icon.
117 state_ = STATE_NOT_ANIMATING;
118 delegate_->ScheduleAudioIndicatorPaint();
119 }
120 }
121
122 std::vector<int> TabAudioIndicator::GetCurrentEqualizerLevels() const {
123 int next_frame_index = (frame_index_ + 1) % arraysize(kEqualizerFrames);
124 std::vector<int> levels;
125 // For all 3 columsn of the equalizer, tween between the current equalizer
126 // level and the target equalizer level.
127 for (size_t i = 0; i < kEqualizerColumnCount; ++i) {
128 int start = kEqualizerFrames[frame_index_][i];
129 int end = state_ == STATE_ANIMATION_ENDING
130 ? 0
131 : kEqualizerFrames[next_frame_index][i];
132 levels.push_back(animation_->CurrentValueBetween(start, end));
133 }
134 return levels;
135 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698