Skip to content

Commit 50a9541

Browse files
authored
Merge pull request scratchfoundation#4338 from paulkaplan/fix-sound-recorder-issues
Fix sound recorder issues
2 parents 9d98d76 + b2117d7 commit 50a9541

File tree

6 files changed

+25
-7
lines changed

6 files changed

+25
-7
lines changed

src/containers/record-modal.jsx

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,9 @@ class RecordModal extends React.Component {
4343
this.setState({recording: true});
4444
}
4545
handleStopRecording (samples, sampleRate, levels, trimStart, trimEnd) {
46-
this.setState({samples, sampleRate, levels, trimStart, trimEnd, recording: false});
46+
if (samples.length > 0) {
47+
this.setState({samples, sampleRate, levels, trimStart, trimEnd, recording: false});
48+
}
4749
}
4850
handlePlay () {
4951
this.setState({playing: true});

src/containers/recording-step.jsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ class RecordingStep extends React.Component {
4141
this.setState({listening: true});
4242
}
4343
handleRecordingError () {
44-
alert(this.props.intl.formatMessage(messages.extensionUrl)); // eslint-disable-line no-alert
44+
alert(this.props.intl.formatMessage(messages.alertMsg)); // eslint-disable-line no-alert
4545
}
4646
handleLevelUpdate (level) {
4747
this.setState({level});

src/containers/sound-editor.jsx

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -120,8 +120,10 @@ class SoundEditor extends React.Component {
120120
const sampleCount = samples.length;
121121
const startIndex = Math.floor(this.state.trimStart * sampleCount);
122122
const endIndex = Math.floor(this.state.trimEnd * sampleCount);
123-
const clippedSamples = samples.slice(startIndex, endIndex);
124-
this.submitNewSamples(clippedSamples, sampleRate);
123+
if (endIndex > startIndex) { // Strictly greater to prevent 0 sample sounds
124+
const clippedSamples = samples.slice(startIndex, endIndex);
125+
this.submitNewSamples(clippedSamples, sampleRate);
126+
}
125127
}
126128
}
127129
handleUpdateTrimEnd (trimEnd) {

src/lib/audio/audio-recorder.js

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -96,8 +96,14 @@ class AudioRecorder {
9696
}
9797
}
9898

99-
const trimStart = Math.max(2, firstChunkAboveThreshold - 2) / this.buffers.length;
100-
const trimEnd = Math.min(this.buffers.length - 2, lastChunkAboveThreshold + 2) / this.buffers.length;
99+
let trimStart = Math.max(2, firstChunkAboveThreshold - 2) / this.buffers.length;
100+
let trimEnd = Math.min(this.buffers.length - 2, lastChunkAboveThreshold + 2) / this.buffers.length;
101+
102+
// With very few samples, the automatic trimming can produce invalid values
103+
if (trimStart >= trimEnd) {
104+
trimStart = 0;
105+
trimEnd = 1;
106+
}
101107

102108
const buffer = new Float32Array(this.buffers.length * this.bufferLength);
103109

test/helpers/selenium-helper.js

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,10 @@ class SeleniumHelper {
4848
if (USE_HEADLESS) {
4949
args.push('--headless');
5050
}
51+
52+
// Stub getUserMedia to always not allow access
53+
args.push('--use-fake-ui-for-media-stream=deny');
54+
5155
chromeCapabilities.set('chromeOptions', {args});
5256
chromeCapabilities.setLoggingPrefs({
5357
performance: 'ALL'

test/integration/blocks.test.js

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -158,14 +158,18 @@ describe('Working with the blocks', () => {
158158
await expect(logs).toEqual([]);
159159
});
160160

161-
test('Record option from sound block menu opens sound recorder', async () => {
161+
test.only('Record option from sound block menu opens sound recorder', async () => {
162162
await loadUri(uri);
163163
await clickXpath('//button[@title="Try It"]');
164164
await clickText('Code');
165165
await clickText('Sound', scope.blocksTab);
166166
await new Promise(resolve => setTimeout(resolve, 1000)); // Wait for scroll animation
167167
await clickText('Meow', scope.blocksTab); // Click "play sound <Meow> until done" block
168168
await clickText('record'); // Click "record..." option in the block's sound menu
169+
// Access has been force denied, so close the alert that comes up
170+
await driver.sleep(1000); // getUserMedia requests are very slow to fail for some reason
171+
await driver.switchTo().alert()
172+
.accept();
169173
await findByText('Record Sound'); // Sound recorder is open
170174
const logs = await getLogs();
171175
await expect(logs).toEqual([]);

0 commit comments

Comments
 (0)