Skip to content
Open
Show file tree
Hide file tree
Changes from 12 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -23,3 +23,9 @@
/.node_modules.ember-try/
/bower.json.ember-try
/package.json.ember-try

# capacitor build files
/ios/
/android/
/electron/
/.gradle*
50 changes: 37 additions & 13 deletions app/components/audio-capturer.js
Original file line number Diff line number Diff line change
@@ -1,27 +1,51 @@
import Component from '@glimmer/component';
import { action } from '@ember/object';
import { Plugins } from '@capacitor/core';
const { VoiceRecorder, Device } = Plugins;
import { Base64Binary } from 'wuf/utils/base64Binary';

export default class AudioCapturerComponent extends Component {
@action
async startRecording() {
const stream = await navigator.mediaDevices.getUserMedia({
audio: true,
video: false
});
const options = { mimeType: 'audio/webm' };
this.mediaRecorder = new MediaRecorder(stream, options);
const deviceInfo = await Device.getInfo();

this.mediaRecorder.addEventListener('dataavailable', e => {
this.args.uploadAudioVideo({ blob: e.data });
});
if (deviceInfo.operatingSystem === 'ios') {
const canRecord = await VoiceRecorder.canDeviceVoiceRecord();
if (canRecord) {
const permissionGranted = await VoiceRecorder.requestAudioRecordingPermission();
if (permissionGranted) {
await VoiceRecorder.startRecording();
}
}
} else {
const stream = await navigator.mediaDevices.getUserMedia({
audio: true,
video: false
});
const options = { mimeType: 'audio/webm' };
this.mediaRecorder = new MediaRecorder(stream, options);

this.mediaRecorder.start();
this.mediaRecorder.addEventListener('dataavailable', e => {
this.args.uploadAudioVideo({ blob: e.data });
});

this.mediaRecorder.start();
}
}

@action
stopRecording() {
if (this.mediaRecorder) {
this.mediaRecorder.stop();
async stopRecording() {
const deviceInfo = await Device.getInfo();

if (deviceInfo.operatingSystem === 'ios') {
let result = await VoiceRecorder.stopRecording();
let byteArray = Base64Binary.decodeArrayBuffer(result.value.recordDataBase64);

this.args.uploadAudioVideo(byteArray);
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@jaredgalanis shouldn't this match the format from before where it's an object with blob as the key? this.args.uploadAudioVideo({ blob: e.data });

I think if we pass the same data format, we can ensure that it should draw the same.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

So I tried passing instantiating a Blob with the ArrayBuffer that we get out of converting the base64 string with this in mind, but it didn't work unfortunately.

} else {
if (this.mediaRecorder) {
this.mediaRecorder.stop();
}
}
}
}
2 changes: 1 addition & 1 deletion app/components/bark-type.hbs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
<div class="flex h-full items-center justify-center w-full">
{{#animated-if @barkType use=this.fade duration=1000}}
<div class="mr-4">
{{svg-jar @barkType height="200" width="200"}}
{{svg-jar @barkType class="h-48 max-w-sm w-full"}}
</div>
<div class="flex-grow">
<h2 class="font-bold text-3xl">
Expand Down
2 changes: 1 addition & 1 deletion app/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<title>Wuf</title>
<meta name="description" content="">
<meta name="viewport" content="width=device-width, initial-scale=1">
<meta name="viewport" content="width=device-width, initial-scale=1, viewport-fit=cover">
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What does this do? I haven't used it before.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is part of what allows the app to respect the safeareas of the notch in an iPhone X or 11.


{{content-for "head"}}

Expand Down
32 changes: 22 additions & 10 deletions app/services/audio-analyzer.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ import {
determineBarkType,
getTimeDomainMaxMin
} from 'wuf/utils/barks';
import { Plugins } from '@capacitor/core';
const { Device } = Plugins;

const barkDescriptions = {
alert:
Expand All @@ -33,7 +35,7 @@ export default class AudioAnalyzerService extends Service {
@action
async analyseAudio(buffer) {
// 44100 hz is the sample rate equivalent to CD audio
const offline = new OfflineAudioContext(2, buffer.length, 44100);
const offline = new (window.OfflineAudioContext || window.webkitOfflineAudioContext)(2, buffer.length, 44100);
const bufferSource = offline.createBufferSource();
bufferSource.onended = () => {
this.barkType = determineBarkType(this.barksOccurred, this.pitches);
Expand All @@ -51,7 +53,7 @@ export default class AudioAnalyzerService extends Service {
this.amplitudeArray = new Uint8Array(analyser.frequencyBinCount);
// The buckets of the array range from 0-22050 Hz, with each bucket representing ~345 Hz
this.frequencyArray = new Uint8Array(analyser.frequencyBinCount);

scp.onaudioprocess = () => {
analyser.getByteTimeDomainData(this.amplitudeArray);
analyser.getByteFrequencyData(this.frequencyArray);
Expand Down Expand Up @@ -123,15 +125,25 @@ export default class AudioAnalyzerService extends Service {
* @param {*} file
*/
@action
async uploadAudioVideo(file) {
async uploadAudioVideo(data) {
this.clearBarkData();
this.clearCanvas();
const fileReader = new FileReader();
fileReader.onload = async ev => {
this.audioContext = new AudioContext();
const buffer = await this.audioContext.decodeAudioData(ev.target.result);
this.analyseAudio(buffer);
};
return fileReader.readAsArrayBuffer(file.blob);
const deviceInfo = await Device.getInfo();

if (deviceInfo.operatingSystem === 'ios') {
let audioCtx = new window.webkitAudioContext();

audioCtx.decodeAudioData(data, (buffer) => {
this.analyseAudio(buffer);
});
} else {
const fileReader = new FileReader();
fileReader.onload = async ev => {
this.audioContext = new AudioContext();
const buffer = await this.audioContext.decodeAudioData(ev.target.result);
this.analyseAudio(buffer);
};
return fileReader.readAsArrayBuffer(data.blob);
}
}
}
2 changes: 2 additions & 0 deletions app/styles/app.scss
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@
body {
@apply text-main;
font-family: 'Poppins', sans-serif;
padding-top: env(safe-area-inset-top);
padding-bottom: env(safe-area-inset-bottom);
}

.animated-container {
Expand Down
2 changes: 1 addition & 1 deletion app/templates/microphone.hbs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
</h2>

<div class="flex justify-center">
{{svg-jar "bath" class="h-auto max-w-sm w-full"}}
{{svg-jar "bath" class="h-64 md:h-auto max-w-sm w-full"}}
</div>

<AudioCapturer
Expand Down
2 changes: 1 addition & 1 deletion app/templates/upload.hbs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
</h2>

<div class="flex justify-center">
{{svg-jar "house" class="h-auto max-w-sm w-full"}}
{{svg-jar "house" class="h-64 md:h-auto max-w-sm w-full"}}
</div>

<AudioUploader
Expand Down
92 changes: 92 additions & 0 deletions app/utils/base64Binary.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,92 @@
/*
Copyright (c) 2011, Daniel Guerrero
All rights reserved.
Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions are met:
* Redistributions of source code must retain the above copyright
notice, this list of conditions and the following disclaimer.
* Redistributions in binary form must reproduce the above copyright
notice, this list of conditions and the following disclaimer in the
documentation and/or other materials provided with the distribution.
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
DISCLAIMED. IN NO EVENT SHALL DANIEL GUERRERO BE LIABLE FOR ANY
DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/

/**
* Uses the new array typed in javascript to binary base64 encode/decode
* at the moment just decodes a binary base64 encoded
* into either an ArrayBuffer (decodeArrayBuffer)
* or into an Uint8Array (decode)
*
* References:
* https://developer.mozilla.org/en/JavaScript_typed_arrays/ArrayBuffer
* https://developer.mozilla.org/en/JavaScript_typed_arrays/Uint8Array
*/

export const Base64Binary = {
_keyStr : 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/=',

/* will return a Uint8Array type */
decodeArrayBuffer: function(input) {
var bytes = (input.length/4) * 3;
var ab = new ArrayBuffer(bytes);
this.decode(input, ab);

return ab;
},

removePaddingChars: function(input){
var lkey = this._keyStr.indexOf(input.charAt(input.length - 1));
if(lkey == 64){
return input.substring(0,input.length - 1);
}
return input;
},

decode: function (input, arrayBuffer) {
//get last chars to see if are valid
input = this.removePaddingChars(input);
input = this.removePaddingChars(input);

var bytes = parseInt((input.length / 4) * 3, 10);

var uarray;
var chr1, chr2, chr3;
var enc1, enc2, enc3, enc4;
var i = 0;
var j = 0;

if (arrayBuffer)
uarray = new Uint8Array(arrayBuffer);
else
uarray = new Uint8Array(bytes);

input = input.replace(/[^A-Za-z0-9+/=]/g, '');

for (i=0; i<bytes; i+=3) {
//get the 3 octects in 4 ascii chars
enc1 = this._keyStr.indexOf(input.charAt(j++));
enc2 = this._keyStr.indexOf(input.charAt(j++));
enc3 = this._keyStr.indexOf(input.charAt(j++));
enc4 = this._keyStr.indexOf(input.charAt(j++));

chr1 = (enc1 << 2) | (enc2 >> 4);
chr2 = ((enc2 & 15) << 4) | (enc3 >> 2);
chr3 = ((enc3 & 3) << 6) | enc4;

uarray[i] = chr1;
if (enc3 != 64) uarray[i+1] = chr2;
if (enc4 != 64) uarray[i+2] = chr3;
}

return uarray;
}
}
20 changes: 20 additions & 0 deletions capacitor.config.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
{
"appId": "com.example.app",
"appName": "ember-capacitor-app",
"bundledWebRuntime": false,
"npmClient": "yarn",
"webDir": "dist",
"server": {
"url": "http://localhost:4200",
"allowNavigation": [
"example.org",
"*.example.org"
]
},
"plugins": {
"SplashScreen": {
"launchShowDuration": 0
}
},
"cordova": {}
}
11 changes: 11 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@
},
"devDependencies": {
"@babel/plugin-proposal-object-rest-spread": "^7.8.3",
"@capacitor/cli": "^2.2.0",
"@capacitor/core": "^2.2.0",
"@ember/optional-features": "^1.3.0",
"@glimmer/component": "^1.0.0",
"@glimmer/tracking": "^1.0.0",
Expand All @@ -29,6 +31,7 @@
"ember-cli": "~3.16.0",
"ember-cli-app-version": "^3.2.0",
"ember-cli-babel": "^7.17.2",
"ember-cli-capacitor": "^0.0.4",
"ember-cli-dependency-checker": "^3.2.0",
"ember-cli-eslint": "^5.1.0",
"ember-cli-fastboot": "^2.2.1",
Expand Down Expand Up @@ -67,5 +70,13 @@
},
"ember": {
"edition": "octane"
},
"dependencies": {
"@capacitor/android": "2.2.0",
"@capacitor/ios": "2.2.0",
"@ionic-native/core": "^5.27.0",
"@ionic-native/file": "^5.27.0",
"@ionic-native/media": "^5.27.0",
"capacitor-voice-recorder": "^0.0.91"
}
}
Loading