登录
首页 >  Golang >  Go问答

使用 Go 语言进行声音检测

来源:stackoverflow

时间:2024-03-12 16:21:26 496浏览 收藏

知识点掌握了,还需要不断练习才能熟练运用。下面golang学习网给大家带来一个Golang开发实战,手把手教大家学习《使用 Go 语言进行声音检测》,在实现功能的过程中也带大家重新温习相关知识点,温故而知新,回头看看说不定又有不一样的感悟!

问题内容

我正在致力于为 golang 声音检测器创建一个非常简单且轻量级的解决方案。我需要读取 mp3/mp4 音频流(目前使用文件)并执行当检测到比平时更大的声音时所具有的功能。我对 golang 比较陌生,对数字音频没有经验。我有下面的代码可以检测蜂鸣声,但不知道如何检测更大的噪音而不仅仅是蜂鸣声。任何帮助将不胜感激!!

package main

import (
    "encoding/binary"
    "fmt"
    "log"
    "math"
    "math/cmplx"
    "net/http"

    "os"

    "github.com/hajimehoshi/go-mp3"
    "github.com/hajimehoshi/oto"

    "github.com/mjibson/go-dsp/fft"

    "github.com/mjibson/go-dsp/window"
)

// const sampleRate = 44100
const toneFrequency = 440
const mp3file = "C:/Users/mmekaiel/Music/audio-samples/440Hz.mp3"

func serveHome(w http.ResponseWriter, r *http.Request) {
    w.Header().Set("Content-Type", "text/html")
    fmt.Fprint(w, "

Hello world site!!

") } func serveAudio(w http.ResponseWriter, r *http.Request) { w.Write([]byte("

The tone-detect audio page!!

")) if err := process(toneFrequency); err != nil { log.Fatal(err) } } func process(toneFreq int) error { f, err := os.Open(mp3file) if err != nil { return err } defer f.Close() d, err := mp3.NewDecoder(f) if err != nil { return err } p, err := oto.NewPlayer(d.SampleRate(), 2, 2, 8192) if err != nil { return err } defer p.Close() windowSize := 40 // window size in milliseconds windowSamples := int(float32(d.SampleRate()) * float32(windowSize) / 1000.0) // fftSize is the smallest power of 2 greater than or equal to windowSamples fftSize := int(math.Pow(2, math.Ceil(math.Log2(float64(windowSamples))))) spectralWidth := float64(d.SampleRate()) / float64(fftSize) targetIndex := int(float64(toneFreq) / spectralWidth) fmt.Printf("Sample Rate: %d\n", d.SampleRate()) fmt.Printf("Length: %d[bytes]\n", d.Length()) fmt.Printf("Window size: %d[samples]\n", windowSamples) fmt.Printf("FFT size: %d\n", fftSize) fmt.Printf("Spectral Line width: %v[hertz]\n", spectralWidth) fmt.Printf("Tone index: %d\n", targetIndex) b := make([]byte, windowSamples*4) // 2 bytes per sample, 2 channels w := make([]float64, fftSize) t := 0 toneStart := -1 outerloop: for { // Read a window of samples bytesRead := 0 for bytesRead < len(b) { n, err := d.Read(b[bytesRead:]) if err != nil { break outerloop } bytesRead += n } // Convert to float (ignore second channel) for i := 0; i < len(b); i += 4 { w[i/4] = float64(int16(binary.LittleEndian.Uint16(b[i+0:i+2]))) / 32768.0 } // Apply window function window.Apply(w, window.Hamming) // Perform FFT c := fft.FFTReal(w) // Compute the normalized magnitude r, _ := cmplx.Polar(c[targetIndex]) r = r / float64(fftSize) // Look for tone toneDetected := r > 0.05 // Apply arbitrary threshold if toneDetected && toneStart < 0 { toneStart = t } else if !toneDetected && (toneStart >= 0) { fmt.Printf("Tone from %dms to %dms.\n", toneStart, t) toneStart = -1 } t += windowSize } return nil } func main() { mux := &http.ServeMux{} mux.HandleFunc("/", serveHome) mux.HandleFunc("/audio", serveAudio) http.ListenAndServe(":8080", mux) }

解决方案


您使用的mp3库将mp3解码为"Raw"音频,通常是PCMNewDecoder的文档也提到了

从中,您可以查找声音数据的确切格式。看起来就很简单,就是左16位,右16位,重复。该值将转换为扬声器振膜的位置。

要查找音量,您应该查找较大的值,或者自上一帧以来值的较大变化。你不需要 FTT 的东西。这是一个名为“fast Fourier transform”的操作。它可以让您查看声音中当前的频率。这对于确定音符的音高很有用,但对于确定音量则毫无用处。我会删除解码器之后的所有内容,并尝试从中读取一些字节。

编辑:经过进一步思考,响亮的声音发生的时间比单个样本要长得多(数字音频不是我的领域)。 -This is how you actually do it-

另请注意:16 位可以有符号或无符号(s16le 或 u16le)。看起来签名是“标准”并且很可能。

编辑 2:除非您正在开发声音处理库,否则我建议您自己寻找一个。我个人使用 GStreamer 来做这类事情,但它对于你的需要来说太过分了(我推荐它)。

也许像 PCM Datatype library 这样的东西。他们似乎在 here 中做了一些增益放大和缩放。看起来是一个挖掘的好地方。

好了,本文到此结束,带大家了解了《使用 Go 语言进行声音检测》,希望本文对你有所帮助!关注golang学习网公众号,给大家分享更多Golang知识!

声明:本文转载于:stackoverflow 如有侵犯,请联系study_golang@163.com删除
相关阅读
更多>
最新阅读
更多>
课程推荐
更多>