File size: 5,195 Bytes
345e5f1
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
import { useEffect, useRef, forwardRef, useState } from 'react'
import WaveSurfer from 'wavesurfer.js'
import { PITCH_WIDTH } from '../constants'

export type AudioTrackProps = {
  audioUrl: string | null
  muted: boolean
  onSeek: (seconds: number) => void
  mediaElement?: HTMLAudioElement | null
  playheadSeconds: number
  gridSecondWidth: number
  minContentWidth?: number  // Minimum width to match MIDI editor area
}

export const AudioTrack = forwardRef<HTMLDivElement, AudioTrackProps>(
  ({ audioUrl, muted, onSeek, playheadSeconds, gridSecondWidth, minContentWidth = 0 }, ref) => {
    const containerRef = useRef<HTMLDivElement | null>(null)
    const waveRef = useRef<WaveSurfer | null>(null)
    const [waveWidth, setWaveWidth] = useState(0)

    useEffect(() => {
      if (!containerRef.current) return
      if (!audioUrl) {
        try {
          waveRef.current?.destroy()
        } catch {
          // ignore teardown errors
        }
        waveRef.current = null
        setWaveWidth(0)
        return
      }

      let cancelled = false

      // Clean up existing instance
      if (waveRef.current) {
        try {
          waveRef.current.destroy()
        } catch {
          // ignore teardown errors
        }
      }

      waveRef.current = WaveSurfer.create({
        container: containerRef.current,
        waveColor: '#4b64bc',
        progressColor: '#4b64bc',
        cursorColor: 'transparent',
        barWidth: 2,
        barGap: 2,
        height: 60,
        normalize: true,
        minPxPerSec: gridSecondWidth,
        interact: false,
        hideScrollbar: true,
        autoScroll: false,
      })

      waveRef.current.load(audioUrl).catch(() => null)
      waveRef.current.on('error', () => null)

      waveRef.current.on('ready', () => {
        if (cancelled || !waveRef.current) return
        const duration = waveRef.current.getDuration()
        const requiredWidth = duration * gridSecondWidth
        setWaveWidth(requiredWidth)
      })

      return () => {
        cancelled = true
        try {
          waveRef.current?.destroy()
        } catch {
          // ignore teardown errors
        }
        waveRef.current = null
      }
    }, [audioUrl, gridSecondWidth])

    useEffect(() => {
      if (!waveRef.current) return
      waveRef.current.setOptions({
        waveColor: muted ? '#9aa6b2' : '#4b64bc',
        progressColor: muted ? '#c0c9d4' : '#4b64bc',
      })
    }, [muted])

    if (!audioUrl) return null

    // Content width should be at least as wide as MIDI editor
    const contentWidth = Math.max(waveWidth, minContentWidth)

    return (
      <div
        className="audio-track-row"
        style={{ 
          display: 'flex', 
          borderBottom: '1px solid var(--border-soft)',
          height: '70px',
          flexShrink: 0
        }}
      >
        <div
          className="audio-gutter"
          style={{
            width: PITCH_WIDTH,
            flexShrink: 0,
            background: 'var(--panel-strong)',
            borderRight: '1px solid var(--border-subtle)',
            display: 'flex',
            alignItems: 'center',
            justifyContent: 'center',
            fontSize: '11px',
            color: 'var(--text-muted)',
            fontWeight: 600,
          }}
        >
          AUDIO
        </div>

        {/* Scroll Mask - Controlled by parent via ref */}
        <div
          ref={ref}
          className="audio-scroll-mask"
          style={{
            flex: 1,
            overflow: 'hidden',
            position: 'relative',
            background: 'var(--panel-soft)',
          }}
          onClick={(e) => {
            const rect = e.currentTarget.getBoundingClientRect()
            const scrollMask = e.currentTarget as HTMLDivElement
            const x = e.clientX - rect.left + scrollMask.scrollLeft
            const seconds = x / gridSecondWidth
            onSeek(seconds)
          }}
        >
          {/* Container that matches MIDI editor width */}
          <div 
            className="audio-content"
            style={{ 
              width: contentWidth > 0 ? contentWidth : '100%',
              height: '100%',
              position: 'relative'
            }}
          >
            {/* WaveSurfer container - only as wide as audio */}
            <div 
              ref={containerRef} 
              className="wave-container" 
              style={{ 
                width: waveWidth > 0 ? waveWidth : '100%',
                height: '100%',
                position: 'absolute',
                left: 0,
                top: 0
              }} 
            />

            {/* Custom Playhead */}
            <div
              className="audio-playhead"
              style={{
                position: 'absolute',
                top: 0,
                bottom: 0,
                width: '2px',
                background: '#ff7043',
                boxShadow: '0 0 12px rgba(255, 112, 67, 0.6)',
                left: playheadSeconds * gridSecondWidth,
                zIndex: 10,
                pointerEvents: 'none',
              }}
            />
          </div>
        </div>
      </div>
    )
  }
)