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

Side by Side Diff: Source/modules/webaudio/AudioBuffer.cpp

Issue 235653002: WebAudio createBuffer should throw error with 0 channels. (Closed) Base URL: svn://svn.chromium.org/blink/trunk
Patch Set: Created 6 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 | Annotate | Revision Log
« no previous file with comments | « Source/modules/webaudio/AudioBuffer.h ('k') | Source/modules/webaudio/AudioContext.cpp » ('j') | 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) 2010 Google Inc. All rights reserved. 2 * Copyright (C) 2010 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 * 7 *
8 * 1. Redistributions of source code must retain the above copyright 8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer. 9 * notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright 10 * 2. Redistributions in binary form must reproduce the above copyright
(...skipping 14 matching lines...) Expand all
25 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF 25 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
26 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 26 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27 */ 27 */
28 28
29 #include "config.h" 29 #include "config.h"
30 30
31 #if ENABLE(WEB_AUDIO) 31 #if ENABLE(WEB_AUDIO)
32 32
33 #include "modules/webaudio/AudioBuffer.h" 33 #include "modules/webaudio/AudioBuffer.h"
34 34
35 #include "bindings/v8/ExceptionMessages.h"
35 #include "bindings/v8/ExceptionState.h" 36 #include "bindings/v8/ExceptionState.h"
36 #include "core/dom/ExceptionCode.h" 37 #include "core/dom/ExceptionCode.h"
37 #include "platform/audio/AudioBus.h" 38 #include "platform/audio/AudioBus.h"
38 #include "platform/audio/AudioFileReader.h" 39 #include "platform/audio/AudioFileReader.h"
39 #include "modules/webaudio/AudioContext.h" 40 #include "modules/webaudio/AudioContext.h"
40 41
41 namespace WebCore { 42 namespace WebCore {
42 43
43 float AudioBuffer::minAllowedSampleRate() 44 float AudioBuffer::minAllowedSampleRate()
44 { 45 {
45 // crbug.com/344375 46 // crbug.com/344375
46 return 3000; 47 return 3000;
47 } 48 }
48 49
49 float AudioBuffer::maxAllowedSampleRate() 50 float AudioBuffer::maxAllowedSampleRate()
50 { 51 {
51 // Windows can support up to this rate. 52 // Windows can support up to this rate.
52 return 192000; 53 return 192000;
53 } 54 }
54 55
55 PassRefPtr<AudioBuffer> AudioBuffer::create(unsigned numberOfChannels, size_t nu mberOfFrames, float sampleRate) 56 PassRefPtr<AudioBuffer> AudioBuffer::create(unsigned numberOfChannels, size_t nu mberOfFrames, float sampleRate)
56 { 57 {
57 if (sampleRate < minAllowedSampleRate() || sampleRate > maxAllowedSampleRate () || numberOfChannels > AudioContext::maxNumberOfChannels() || !numberOfFrames) 58 if (sampleRate < minAllowedSampleRate() || sampleRate > maxAllowedSampleRate () || numberOfChannels > AudioContext::maxNumberOfChannels() || !numberOfChannel s || !numberOfFrames)
58 return nullptr; 59 return nullptr;
59 60
60 RefPtr<AudioBuffer> buffer = adoptRef(new AudioBuffer(numberOfChannels, numb erOfFrames, sampleRate)); 61 RefPtr<AudioBuffer> buffer = adoptRef(new AudioBuffer(numberOfChannels, numb erOfFrames, sampleRate));
61 62
62 if (!buffer->createdSuccessfully(numberOfChannels)) 63 if (!buffer->createdSuccessfully(numberOfChannels))
63 return nullptr; 64 return nullptr;
64 return buffer; 65 return buffer;
65 } 66 }
66 67
68 PassRefPtr<AudioBuffer> AudioBuffer::create(unsigned numberOfChannels, size_t nu mberOfFrames, float sampleRate, ExceptionState& exceptionState)
69 {
70 if (!numberOfChannels || numberOfChannels > AudioContext::maxNumberOfChannel s()) {
71 exceptionState.throwDOMException(
72 NotSupportedError,
73 ExceptionMessages::indexOutsideRange(
74 "number of channels",
75 numberOfChannels,
76 1u,
77 ExceptionMessages::InclusiveBound,
78 AudioContext::maxNumberOfChannels(),
79 ExceptionMessages::InclusiveBound));
80 return nullptr;
81 }
82
83 if (sampleRate < AudioBuffer::minAllowedSampleRate() || sampleRate > AudioBu ffer::maxAllowedSampleRate()) {
84 exceptionState.throwDOMException(
85 NotSupportedError,
86 ExceptionMessages::indexOutsideRange(
87 "sample rate",
88 sampleRate,
89 AudioBuffer::minAllowedSampleRate(),
90 ExceptionMessages::InclusiveBound,
91 AudioBuffer::maxAllowedSampleRate(),
92 ExceptionMessages::InclusiveBound));
93 return nullptr;
94 }
95
96 if (!numberOfFrames) {
97 exceptionState.throwDOMException(
98 NotSupportedError,
99 ExceptionMessages::indexExceedsMinimumBound(
100 "number of frames",
101 numberOfFrames,
102 static_cast<size_t>(0)));
103 return nullptr;
104 }
105
106 RefPtr<AudioBuffer> audioBuffer = create(numberOfChannels, numberOfFrames, s ampleRate);
107
108 if (!audioBuffer.get()) {
109 exceptionState.throwDOMException(
110 NotSupportedError,
111 "createBuffer("
112 + String::number(numberOfChannels) + ", "
113 + String::number(numberOfFrames) + ", "
114 + String::number(sampleRate)
115 + ") failed.");
116 }
117
118 return audioBuffer;
119 }
120
67 PassRefPtr<AudioBuffer> AudioBuffer::createFromAudioFileData(const void* data, s ize_t dataSize, bool mixToMono, float sampleRate) 121 PassRefPtr<AudioBuffer> AudioBuffer::createFromAudioFileData(const void* data, s ize_t dataSize, bool mixToMono, float sampleRate)
68 { 122 {
69 RefPtr<AudioBus> bus = createBusFromInMemoryAudioFile(data, dataSize, mixToM ono, sampleRate); 123 RefPtr<AudioBus> bus = createBusFromInMemoryAudioFile(data, dataSize, mixToM ono, sampleRate);
70 if (bus.get()) { 124 if (bus.get()) {
71 RefPtr<AudioBuffer> buffer = adoptRef(new AudioBuffer(bus.get())); 125 RefPtr<AudioBuffer> buffer = adoptRef(new AudioBuffer(bus.get()));
72 if (buffer->createdSuccessfully(bus->numberOfChannels())) 126 if (buffer->createdSuccessfully(bus->numberOfChannels()))
73 return buffer; 127 return buffer;
74 } 128 }
75 129
76 return nullptr; 130 return nullptr;
(...skipping 68 matching lines...) Expand 10 before | Expand all | Expand 10 after
145 { 199 {
146 for (unsigned i = 0; i < m_channels.size(); ++i) { 200 for (unsigned i = 0; i < m_channels.size(); ++i) {
147 if (getChannelData(i)) 201 if (getChannelData(i))
148 getChannelData(i)->zeroRange(0, length()); 202 getChannelData(i)->zeroRange(0, length());
149 } 203 }
150 } 204 }
151 205
152 } // namespace WebCore 206 } // namespace WebCore
153 207
154 #endif // ENABLE(WEB_AUDIO) 208 #endif // ENABLE(WEB_AUDIO)
OLDNEW
« no previous file with comments | « Source/modules/webaudio/AudioBuffer.h ('k') | Source/modules/webaudio/AudioContext.cpp » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698