refactor: eliminate dead code and extract common patterns#27
Open
flexiondotorg wants to merge 7 commits intomainfrom
Open
refactor: eliminate dead code and extract common patterns#27flexiondotorg wants to merge 7 commits intomainfrom
flexiondotorg wants to merge 7 commits intomainfrom
Conversation
…h stdlib - Replace 4 unsafe.Pointer float32 casts in ffmpeg_decoder.go with math.Float32frombits() - Replace 3 unsafe.Pointer float32 casts in encoder.go with math.Float32bits() + binary.LittleEndian - Remove unnecessary unsafe import from ffmpeg_decoder.go - Improve code clarity by leveraging stable stdlib functions available since Go 1.0 Signed-off-by: Martin Wimpress <code@wimpress.io>
Replace manual magnitude calculations with math/cmplx.Abs: - fft.go: Replace sqrt(real²+imag²) with cmplx.Abs - analyzer.go: Replace sqrt(real²+imag²) with cmplx.Abs - Add math/cmplx imports where needed Improves clarity and uses stdlib functionality available since Go 1.0. Signed-off-by: Martin Wimpress <code@wimpress.io>
- Delete unused file internal/audio/shared_buffer.go (240 lines) - Remove unused functions: ApplyHanning, SeekToSample, NumChannels, WriteFrame, convertRGBToYUV, EncoderDescription, IsHardwareAccelerated, GetVideoFramesEncoded, GetEncoderStatus, Frame.Release, AudioFlushProgress, and 10 CLI style functions - Remove unused imports: "io" from decoder, "strings" from hwaccel, "sync" from renderer - Delete 14 test functions and benchmarks depending on removed code - Rewrite 6 tests to use live code paths Signed-off-by: Martin Wimpress <code@wimpress.io>
Extract FillFFTBuffer and ReadNextFrame helper functions from AnalyzeAudio and runPass2 to eliminate duplicate read-loop logic. Both functions now call shared implementations in buffer.go with full unit test coverage. Signed-off-by: Martin Wimpress <code@wimpress.io>
Create `openAudioFormatCtx` in ffmpeg_common.go to consolidate the duplicated stream-search logic that appeared in both NewFFmpegDecoder and GetMetadata. Both callers now invoke the shared function instead of maintaining identical 7-line loops inline. Signed-off-by: Martin Wimpress <code@wimpress.io>
…ment
Consolidate two recurring code patterns from the renderer package:
- Extract loadImageData helper to handle filesystem-vs-embed branching
for both LoadBackgroundImage and loadThumbnailBackground callers
- Extract text drawing helpers (newTextDrawer, measureStringWidth,
measureTextBounds) from assets.go and thumbnail.go into text.go
- Replace duplicated &font.Drawer{} construction and text measurement
boilerplate with reusable functions
Signed-off-by: Martin Wimpress <code@wimpress.io>
- Add outputChannels() method to consolidate audio channel defaulting logic used in three encoder functions - Extract writeRow and writeBarRow closures in progress.go to replace ten repetitive fmt.Fprintf blocks in completion table rendering - Add mustAnalyze(t) test helper to replace six instances of duplicated AnalyzeAudio setup boilerplate in analyzer_test.go Signed-off-by: Martin Wimpress <code@wimpress.io>
There was a problem hiding this comment.
2 issues found across 28 files
Confidence score: 3/5
- There is some merge risk:
ReadNextFrameincmd/jivefire/main.gonow allocates per frame inside the render loop, which is a concrete performance regression from the prior no-allocation audio path. - The test issue in
internal/audio/buffer_test.golowers confidence becauseTestReadNextFramePartialAtEOFmay pass without proving partial-frame EOF behavior, so future regressions could slip through. - Given the moderate severity and high confidence on both findings, this looks manageable but worth addressing before relying on performance-sensitive behavior.
- Pay close attention to
cmd/jivefire/main.goandinternal/audio/buffer_test.go- render-loop allocation regressions and incomplete EOF partial-read assertions are the main risk areas.
Prompt for AI agents (unresolved issues)
Check if these issues are valid — if so, understand the root cause of each and fix them. If appropriate, use sub-agents to investigate and fix each issue separately.
<file name="internal/audio/buffer_test.go">
<violation number="1" location="internal/audio/buffer_test.go:114">
P2: `TestReadNextFramePartialAtEOF` can pass without ever asserting that a partial frame was returned, so it may miss regressions in EOF partial-read behavior.</violation>
</file>
<file name="cmd/jivefire/main.go">
<violation number="1" location="cmd/jivefire/main.go:547">
P2: `ReadNextFrame` introduces per-frame slice allocations in the render loop, regressing the previous no-allocation audio read path.</violation>
</file>
Reply with feedback, questions, or to request a fix. Tag @cubic-dev-ai to re-run a review.
| // Drain most of the file | ||
| for { | ||
| samples, err := ReadNextFrame(reader, 4096) | ||
| if errors.Is(err, io.EOF) { |
There was a problem hiding this comment.
P2: TestReadNextFramePartialAtEOF can pass without ever asserting that a partial frame was returned, so it may miss regressions in EOF partial-read behavior.
Prompt for AI agents
Check if this issue is valid — if so, understand the root cause and fix it. At internal/audio/buffer_test.go, line 114:
<comment>`TestReadNextFramePartialAtEOF` can pass without ever asserting that a partial frame was returned, so it may miss regressions in EOF partial-read behavior.</comment>
<file context>
@@ -0,0 +1,156 @@
+ // Drain most of the file
+ for {
+ samples, err := ReadNextFrame(reader, 4096)
+ if errors.Is(err, io.EOF) {
+ break
+ }
</file context>
| cli.PrintError(fmt.Sprintf("error reading audio: %v", err)) | ||
| p.Quit() | ||
| return | ||
| newSamples, readErr := audio.ReadNextFrame(reader, samplesPerFrame) |
There was a problem hiding this comment.
P2: ReadNextFrame introduces per-frame slice allocations in the render loop, regressing the previous no-allocation audio read path.
Prompt for AI agents
Check if this issue is valid — if so, understand the root cause and fix it. At cmd/jivefire/main.go, line 547:
<comment>`ReadNextFrame` introduces per-frame slice allocations in the render loop, regressing the previous no-allocation audio read path.</comment>
<file context>
@@ -556,31 +544,15 @@ func runPass2(p *tea.Program, inputFile string, outputFile string, channels int,
- cli.PrintError(fmt.Sprintf("error reading audio: %v", err))
- p.Quit()
- return
+ newSamples, readErr := audio.ReadNextFrame(reader, samplesPerFrame)
+ if readErr != nil {
+ if errors.Is(readErr, io.EOF) {
</file context>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
This PR addresses three major refactoring objectives: modernising stdlib usage patterns, removing 14 instances of dead code that accumulated during development, and extracting repeated logic into shared helper functions across the codebase. The changes reduce code complexity, improve maintainability, and eliminate unused exports that cluttered the API surface.
Changes
unsafe.Pointerfloat32 conversions withmath.Float32bitsandmath.Float32frombitsfrom the standard librarycmplx.Absfor complex number magnitude calculations instead of manual computationSharedAudioBuffer(390 lines),ApplyHanning,WriteFrame,convertRGBToYUV,GetEncoderStatus,Frame.Release,AudioFlushProgress, 10 CLI style functions, and minor unused exports (net -1,306 lines)buffer.go:FillFFTBufferandReadNextFramehelpers eliminate duplication in FFT analysisffmpeg_common.go:openAudioFormatCtxconsolidates format context initialisation across decoder and analysertext.go:loadImageData,newTextDrawer,measureStringWidth,measureTextBoundseliminate duplication in asset loading and text measurementoutputChannels(),writeRow/writeBarRowclosures, andmustAnalyzetest helperTesting
buffer_test.gocover extracted buffer helpers