1 // Copyright (c) 2009-2010, Niels Martin Hansen
2 // All rights reserved.
3 //
4 // Redistribution and use in source and binary forms, with or without
5 // modification, are permitted provided that the following conditions are met:
6 //
7 // * Redistributions of source code must retain the above copyright notice,
8 // this list of conditions and the following disclaimer.
9 // * Redistributions in binary form must reproduce the above copyright notice,
10 // this list of conditions and the following disclaimer in the documentation
11 // and/or other materials provided with the distribution.
12 // * Neither the name of the Aegisub Group nor the names of its contributors
13 // may be used to endorse or promote products derived from this software
14 // without specific prior written permission.
15 //
16 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
17 // AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18 // IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19 // ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
20 // LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
21 // CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
22 // SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
23 // INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
24 // CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
25 // ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
26 // POSSIBILITY OF SUCH DAMAGE.
27 //
28 // Aegisub Project http://www.aegisub.org/
29
30 /// @file audio_renderer.cpp
31 /// @brief Base classes for audio renderers (spectrum, waveform, ...)
32 /// @ingroup audio_ui
33
34 #include "audio_renderer.h"
35
36 #include <libaegisub/audio/provider.h>
37 #include <libaegisub/make_unique.h>
38
39 #include <algorithm>
40 #include <wx/dc.h>
41
42 namespace {
43 template<typename T>
compare_and_set(T & var,T new_value)44 bool compare_and_set(T &var, T new_value)
45 {
46 if (var == new_value) return false;
47 var = new_value;
48 return true;
49 }
50 }
51
AudioRendererBitmapCacheBitmapFactory(AudioRenderer * renderer)52 AudioRendererBitmapCacheBitmapFactory::AudioRendererBitmapCacheBitmapFactory(AudioRenderer *renderer)
53 : renderer(renderer)
54 {
55 assert(renderer);
56 }
57
ProduceBlock(int)58 std::unique_ptr<wxBitmap> AudioRendererBitmapCacheBitmapFactory::ProduceBlock(int /* i */)
59 {
60 return agi::make_unique<wxBitmap>(renderer->cache_bitmap_width, renderer->pixel_height, 24);
61 }
62
GetBlockSize() const63 size_t AudioRendererBitmapCacheBitmapFactory::GetBlockSize() const
64 {
65 return sizeof(wxBitmap) + renderer->cache_bitmap_width * renderer->pixel_height * 3;
66 }
67
AudioRenderer()68 AudioRenderer::AudioRenderer()
69 {
70 for (int i = 0; i < AudioStyle_MAX; ++i)
71 bitmaps.emplace_back(256, AudioRendererBitmapCacheBitmapFactory(this));
72
73 // Make sure there's *some* values for those fields, and in the caches
74 SetMillisecondsPerPixel(1);
75 SetHeight(1);
76 }
77
SetMillisecondsPerPixel(double new_pixel_ms)78 void AudioRenderer::SetMillisecondsPerPixel(double new_pixel_ms)
79 {
80 if (compare_and_set(pixel_ms, new_pixel_ms))
81 {
82 if (renderer)
83 renderer->SetMillisecondsPerPixel(pixel_ms);
84
85 ResetBlockCount();
86 }
87 }
88
SetHeight(int _pixel_height)89 void AudioRenderer::SetHeight(int _pixel_height)
90 {
91 if (compare_and_set(pixel_height, _pixel_height))
92 Invalidate();
93 }
94
SetAmplitudeScale(float _amplitude_scale)95 void AudioRenderer::SetAmplitudeScale(float _amplitude_scale)
96 {
97 if (compare_and_set(amplitude_scale, _amplitude_scale))
98 {
99 // A scaling of 0 or a negative scaling makes no sense
100 assert(amplitude_scale > 0);
101 if (renderer)
102 renderer->SetAmplitudeScale(amplitude_scale);
103 Invalidate();
104 }
105 }
106
SetRenderer(AudioRendererBitmapProvider * _renderer)107 void AudioRenderer::SetRenderer(AudioRendererBitmapProvider *_renderer)
108 {
109 if (compare_and_set(renderer, _renderer))
110 {
111 Invalidate();
112
113 if (renderer)
114 {
115 renderer->SetProvider(provider);
116 renderer->SetAmplitudeScale(amplitude_scale);
117 renderer->SetMillisecondsPerPixel(pixel_ms);
118 }
119 }
120 }
121
SetAudioProvider(agi::AudioProvider * _provider)122 void AudioRenderer::SetAudioProvider(agi::AudioProvider *_provider)
123 {
124 if (compare_and_set(provider, _provider))
125 {
126 Invalidate();
127
128 if (renderer)
129 renderer->SetProvider(provider);
130
131 ResetBlockCount();
132 }
133 }
134
SetCacheMaxSize(size_t max_size)135 void AudioRenderer::SetCacheMaxSize(size_t max_size)
136 {
137 // Limit the bitmap cache sizes to 16 MB hard, to avoid the risk of exhausting
138 // system bitmap object resources and similar. Experimenting shows that 16 MB
139 // bitmap cache should be plenty even if working with a one hour audio clip.
140 cache_bitmap_maxsize = std::min<size_t>(max_size/8, 0x1000000);
141 // The renderer gets whatever is left.
142 cache_renderer_maxsize = max_size - 4*cache_bitmap_maxsize;
143 }
144
ResetBlockCount()145 void AudioRenderer::ResetBlockCount()
146 {
147 if (provider)
148 {
149 const size_t total_blocks = NumBlocks(provider->GetNumSamples());
150 for (auto& bmp : bitmaps) bmp.SetBlockCount(total_blocks);
151 }
152 }
153
NumBlocks(const int64_t samples) const154 size_t AudioRenderer::NumBlocks(const int64_t samples) const
155 {
156 const double duration = samples * 1000.0 / provider->GetSampleRate();
157 return static_cast<size_t>(duration / pixel_ms / cache_bitmap_width);
158 }
159
GetCachedBitmap(int i,AudioRenderingStyle style)160 const wxBitmap *AudioRenderer::GetCachedBitmap(int i, AudioRenderingStyle style)
161 {
162 assert(provider);
163 assert(renderer);
164
165 bool created = false;
166 wxBitmap *bmp = bitmaps[style].Get(i, &created);
167 assert(bmp);
168 if (created)
169 {
170 renderer->Render(*bmp, i*cache_bitmap_width, style);
171 needs_age = true;
172 }
173
174 assert(bmp->IsOk());
175 return bmp;
176 }
177
Render(wxDC & dc,wxPoint origin,int start,int length,AudioRenderingStyle style)178 void AudioRenderer::Render(wxDC &dc, wxPoint origin, int start, int length, AudioRenderingStyle style)
179 {
180 assert(start >= 0);
181
182 if (!provider) return;
183 if (!renderer) return;
184 if (length <= 0) return;
185
186 // One past last absolute pixel strip to render
187 int end = start + length;
188 // One past last X coordinate to render on
189 int lastx = origin.x + length;
190 // Figure out which range of bitmaps are required
191 int firstbitmap = start / cache_bitmap_width;
192 // And the offset in it to start its use at
193 int firstbitmapoffset = start % cache_bitmap_width;
194 // The last bitmap required
195 int lastbitmap = std::min<int>(end / cache_bitmap_width, NumBlocks(provider->GetDecodedSamples()) - 1);
196
197 // Set a clipping region so that the first and last bitmaps don't draw
198 // outside the requested range
199 wxDCClipper clipper(dc, wxRect(origin, wxSize(length, pixel_height)));
200 origin.x -= firstbitmapoffset;
201
202 for (int i = firstbitmap; i <= lastbitmap; ++i)
203 {
204 dc.DrawBitmap(*GetCachedBitmap(i, style), origin);
205 origin.x += cache_bitmap_width;
206 }
207
208 // Now render blank audio from origin to end
209 if (origin.x < lastx)
210 renderer->RenderBlank(dc, wxRect(origin.x-1, origin.y, lastx-origin.x+1, pixel_height), style);
211
212 if (needs_age)
213 {
214 bitmaps[style].Age(cache_bitmap_maxsize);
215 renderer->AgeCache(cache_renderer_maxsize);
216 needs_age = false;
217 }
218 }
219
Invalidate()220 void AudioRenderer::Invalidate()
221 {
222 for (auto& bmp : bitmaps) bmp.Age(0);
223 needs_age = false;
224 }
225
SetProvider(agi::AudioProvider * _provider)226 void AudioRendererBitmapProvider::SetProvider(agi::AudioProvider *_provider)
227 {
228 if (compare_and_set(provider, _provider))
229 OnSetProvider();
230 }
231
SetMillisecondsPerPixel(double new_pixel_ms)232 void AudioRendererBitmapProvider::SetMillisecondsPerPixel(double new_pixel_ms)
233 {
234 if (compare_and_set(pixel_ms, new_pixel_ms))
235 OnSetMillisecondsPerPixel();
236 }
237
SetAmplitudeScale(float _amplitude_scale)238 void AudioRendererBitmapProvider::SetAmplitudeScale(float _amplitude_scale)
239 {
240 if (compare_and_set(amplitude_scale, _amplitude_scale))
241 OnSetAmplitudeScale();
242 }
243