Skip to content

Commit 8a21ba6

Browse files
authored
audio record fixes (keybase#25169)
1 parent 7070105 commit 8a21ba6

10 files changed

Lines changed: 174 additions & 96 deletions

File tree

‎shared/actions/platform-specific/index.native.tsx‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -780,7 +780,7 @@ const onEnableAudioRecording = async (
780780
bitRate: 32000,
781781
extension: '.m4a',
782782
numberOfChannels: 1,
783-
outputFormat: Audio.RECORDING_OPTION_ANDROID_OUTPUT_FORMAT_AAC_ADTS,
783+
outputFormat: Audio.RECORDING_OPTION_ANDROID_OUTPUT_FORMAT_MPEG_4,
784784
sampleRate: 22050,
785785
},
786786
ios: {
@@ -791,6 +791,7 @@ const onEnableAudioRecording = async (
791791
linearPCMIsBigEndian: false,
792792
linearPCMIsFloat: false,
793793
numberOfChannels: 1,
794+
outputFormat: Audio.RECORDING_OPTION_IOS_OUTPUT_FORMAT_MPEG4AAC,
794795
sampleRate: 22050,
795796
},
796797
isMeteringEnabled: true,

‎shared/chat/audio/amptracker.tsx‎

Lines changed: 32 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
1+
import {maxAmpsLength} from '../../constants/chat2/message'
12
const minBars = 20
2-
const maxBars = 60
3+
const maxBars = maxAmpsLength
34
const snap0 = 1000 // anything under this seconds takes minBars
45
const snap1 = 30000 // anything over this seconds takes maxBars
56

@@ -204,3 +205,33 @@ rescaled = bl.rescaleToNewBucketList(TARGET_BUCKET_DT)
204205
console.log("\n\nRESCALED IN //{Date.now() - startTime}ms:")
205206
rescaled.print()
206207
*/
208+
209+
// const print = (arr: Array<number>) => {
210+
// for (const r of arr) {
211+
// let s = '.'
212+
// for (let i = 0; i < r; ++i) {
213+
// s += 'X'
214+
// }
215+
// console.log(s)
216+
// }
217+
// }
218+
219+
// const dur = 1000
220+
// const tracker = new AmpTracker()
221+
// const raw = new Array<number>()
222+
// for (let i = 0; i < 100; ++i) {
223+
// const section = Math.floor(i / 20)
224+
// raw.push(section % 2 ? 10 : 0)
225+
// }
226+
// console.log('tracker +raw', raw.length)
227+
// print(raw)
228+
// console.log('tracker -raw')
229+
230+
// for (const r of raw) {
231+
// tracker.addAmp(r)
232+
// }
233+
234+
// const after = tracker.getBucketedAmps(dur)
235+
// console.log('tracker +after', after.length)
236+
// print(after)
237+
// console.log('tracker -after')

‎shared/chat/audio/audio-player.tsx‎

Lines changed: 40 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -7,18 +7,24 @@ import {isMobile} from '../../constants/platform'
77

88
type VisProps = {
99
amps: Array<number>
10-
ampsRemain: number
10+
playedRatio: number
1111
height: number
1212
maxWidth?: number
1313
}
1414

1515
const AudioVis = (props: VisProps) => {
16+
const {amps, playedRatio, maxWidth} = props
17+
let threshold = Math.floor(amps.length * playedRatio)
18+
if (threshold > 0) {
19+
threshold += 2 // metering actually lags so compensate while actually playing
20+
}
21+
1622
let maxHeight = 0
17-
const content = props.amps.map((inamp, index) => {
23+
const content = amps.map((inamp, index) => {
1824
const amp = isNaN(inamp) || inamp < 0 ? 0 : inamp
1925
const prop = Math.min(1.0, Math.max(Math.sqrt(amp), 0.05))
2026
const height = Math.floor(prop * props.height)
21-
if (height >= maxHeight) {
27+
if (height > maxHeight) {
2228
maxHeight = height
2329
}
2430
return (
@@ -27,7 +33,7 @@ const AudioVis = (props: VisProps) => {
2733
direction="vertical"
2834
key={index}
2935
style={{
30-
backgroundColor: index < props.ampsRemain ? Styles.globalColors.blue : Styles.globalColors.black,
36+
backgroundColor: index < threshold ? Styles.globalColors.blue : Styles.globalColors.black,
3137
height,
3238
marginRight: isMobile ? 4 * Styles.hairlineWidth : 2,
3339
width: isMobile ? 3 * Styles.hairlineWidth : 1,
@@ -38,15 +44,15 @@ const AudioVis = (props: VisProps) => {
3844
return Styles.isMobile ? (
3945
<Kb.ScrollView
4046
horizontal={true}
41-
style={{height: maxHeight, maxWidth: props.maxWidth}}
47+
style={{height: maxHeight, maxWidth: maxWidth}}
4248
showsHorizontalScrollIndicator={false}
4349
>
4450
{content}
4551
</Kb.ScrollView>
4652
) : (
4753
<Kb.Box2
4854
direction="horizontal"
49-
style={{height: maxHeight, marginTop: Styles.globalMargins.xtiny, maxWidth: props.maxWidth}}
55+
style={{height: maxHeight, marginTop: Styles.globalMargins.xtiny, maxWidth: maxWidth}}
5056
>
5157
{content}
5258
</Kb.Box2>
@@ -62,73 +68,57 @@ type Props = {
6268
}
6369

6470
const AudioPlayer = (props: Props) => {
71+
const {duration, big, maxWidth, url, visAmps} = props
6572
const seekRef = React.useRef<null | ((n: number) => void)>(null)
66-
const [timeStart, setTimeStart] = React.useState(0)
67-
const [timeLeft, setTimeLeft] = React.useState(props.duration)
68-
const [timePaused, setTimePaused] = React.useState(0)
69-
const [timePauseButton, setTimePauseButton] = React.useState(0)
73+
const [playedRatio, setPlayedRatio] = React.useState(0)
7074
const [paused, setPaused] = React.useState(true)
7175
const onClick = () => {
7276
if (paused) {
73-
if (timeStart === 0) {
74-
setTimeStart(Date.now())
75-
} else if (timePauseButton > 0) {
76-
setTimePaused(timePaused + (Date.now() - timePauseButton))
77-
}
7877
setPaused(false)
79-
setTimePauseButton(0)
8078
} else {
81-
setTimePauseButton(Date.now())
8279
setPaused(true)
8380
}
8481
}
85-
React.useEffect(() => {
86-
if (paused) {
87-
return
88-
}
89-
const timer = setTimeout(() => {
90-
const diff = Date.now() - timeStart - timePaused
91-
const newTimeLeft = props.duration - diff
92-
if (newTimeLeft <= 0) {
93-
setTimeLeft(props.duration)
94-
setPaused(true)
95-
setTimeStart(0)
96-
setTimePaused(0)
97-
setTimePauseButton(0)
98-
seekRef.current?.(0)
99-
} else {
100-
setTimeLeft(newTimeLeft)
101-
}
102-
}, 100)
103-
return () => {
104-
clearTimeout(timer)
105-
}
106-
}, [timeLeft, timeStart, timePaused, paused, props.duration])
10782

108-
const ampsRemain = Math.floor((1 - timeLeft / props.duration) * props.visAmps.length)
83+
const onPositionUpdated = React.useCallback(
84+
(ratio: number) => {
85+
setPlayedRatio(ratio)
86+
},
87+
[setPlayedRatio]
88+
)
89+
90+
const onEnded = React.useCallback(() => {
91+
setPaused(true)
92+
setPlayedRatio(0)
93+
}, [setPaused, setPlayedRatio])
94+
95+
const timeLeft = duration - playedRatio * duration
10996
return (
11097
<Kb.Box2
11198
direction="horizontal"
112-
style={Styles.collapseStyles([styles.container, {height: props.big ? 56 : 40}])}
99+
style={Styles.collapseStyles([styles.container, {height: big ? 56 : 40}])}
113100
gap="tiny"
114101
>
115-
<Kb.ClickableBox onClick={props.url ? onClick : undefined} style={{justifyContent: 'center'}}>
102+
<Kb.ClickableBox onClick={url ? onClick : undefined} style={{justifyContent: 'center'}}>
116103
<Kb.Icon
117104
type={!paused ? 'iconfont-pause' : 'iconfont-play'}
118105
fontSize={32}
119-
color={props.url ? Styles.globalColors.blue : Styles.globalColors.grey}
106+
color={url ? Styles.globalColors.blue : Styles.globalColors.grey}
120107
/>
121108
</Kb.ClickableBox>
122109
<Kb.Box2 direction="vertical" style={styles.visContainer} gap="xxtiny" fullHeight={true}>
123-
<AudioVis
124-
height={props.big ? 32 : 18}
125-
amps={props.visAmps}
126-
maxWidth={props.maxWidth}
127-
ampsRemain={ampsRemain}
128-
/>
110+
<AudioVis height={big ? 32 : 18} amps={visAmps} maxWidth={maxWidth} playedRatio={playedRatio} />
129111
<Kb.Text type="BodyTiny">{formatAudioRecordDuration(timeLeft)}</Kb.Text>
130112
</Kb.Box2>
131-
{props.url.length > 0 && <AudioVideo seekRef={seekRef} url={props.url} paused={paused} />}
113+
{url.length > 0 && (
114+
<AudioVideo
115+
seekRef={seekRef}
116+
url={url}
117+
paused={paused}
118+
onPositionUpdated={onPositionUpdated}
119+
onEnded={onEnded}
120+
/>
121+
)}
132122
</Kb.Box2>
133123
)
134124
}

‎shared/chat/audio/audio-send.tsx‎

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,11 +12,8 @@ type Props = {
1212
}
1313

1414
const AudioSend = (props: Props) => {
15-
// props
1615
const {conversationIDKey} = props
17-
// state
1816
const audioRecording = Container.useSelector(state => state.chat2.audioRecording.get(conversationIDKey))
19-
// dispatch
2017
const dispatch = Container.useDispatch()
2118
const onCancel = () => {
2219
dispatch(Chat2Gen.createStopAudioRecording({conversationIDKey, stopType: Types.AudioStopType.CANCEL}))

‎shared/chat/audio/audio-video.d.ts‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@ export type Props = {
44
url: string
55
paused: boolean
66
seekRef: React.MutableRefObject<null | ((s: number) => void)>
7+
onPositionUpdated: (ratio: number) => void
8+
onEnded: () => void
79
}
810

911
export default class extends React.Component<Props> {}

‎shared/chat/audio/audio-video.desktop.tsx‎

Lines changed: 27 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ import * as Container from '../../util/container'
33
import type {Props} from './audio-video'
44

55
const AudioVideo = (props: Props) => {
6-
const {url, seekRef, paused} = props
6+
const {url, seekRef, paused, onPositionUpdated, onEnded} = props
77
const vidRef = React.useRef<HTMLVideoElement | null>(null)
88
const seek = React.useCallback(
99
(seconds: number) => {
@@ -16,9 +16,23 @@ const AudioVideo = (props: Props) => {
1616
},
1717
[vidRef, paused]
1818
)
19-
React.useEffect(() => {
20-
seekRef.current = seek
21-
}, [seekRef, seek])
19+
20+
seekRef.current = seek
21+
const onTimeUpdate = React.useCallback(
22+
(e: any) => {
23+
const ct = e?.target?.currentTime ?? -1
24+
let dur = e?.target?.duration ?? -1
25+
if (dur === 0) {
26+
return
27+
}
28+
onPositionUpdated(ct / dur)
29+
},
30+
[onPositionUpdated]
31+
)
32+
33+
const onEndedRaw = React.useCallback(() => {
34+
onEnded()
35+
}, [onEnded])
2236

2337
const lastPaused = Container.usePrevious(paused)
2438
React.useEffect(() => {
@@ -36,7 +50,15 @@ const AudioVideo = (props: Props) => {
3650
}
3751
}, [paused, lastPaused, vidRef])
3852

39-
return <video ref={vidRef} src={url} style={{height: 0, width: 0}} />
53+
return (
54+
<video
55+
ref={vidRef}
56+
src={url}
57+
style={{height: 0, width: 0}}
58+
onTimeUpdate={onTimeUpdate}
59+
onEnded={onEndedRaw}
60+
/>
61+
)
4062
}
4163

4264
export default AudioVideo

0 commit comments

Comments
 (0)