yinbangzhong
2024-06-17 7342185782fec53480caeba8e047d01b35927bec
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
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
package service
 
import (
    "bytes"
    "context"
    "encoding/json"
    "errors"
    "fmt"
    "github.com/fsnotify/fsnotify"
    "gorm.io/gorm"
    "io"
    "log"
    "mime/multipart"
    "net/http"
    "os"
    "path/filepath"
    "speechAnalysis/conf"
    "speechAnalysis/constvar"
    "speechAnalysis/models"
    "speechAnalysis/pkg/logx"
    "strings"
    "time"
)
 
// Response 结构体用于存储响应体的内容
type Response struct {
    Code   int     `json:"code"`
    Msg    string  `json:"msg"`
    Result string  `json:"result"`
    Score  float64 `json:"score"`
}
 
func AnalysisAudio(filename string, targetURL string) (resp Response, err error) {
    file, err := os.Open(filename)
    if err != nil {
        return
    }
    defer file.Close()
 
    // 创建一个缓冲区来存储表单数据
    var requestBody bytes.Buffer
    writer := multipart.NewWriter(&requestBody)
 
    // 创建一个表单字段,用于存储文件
    fileWriter, err := writer.CreateFormFile("audio", filename)
    if err != nil {
        return
    }
 
    // 将文件内容复制到表单字段中
    _, err = io.Copy(fileWriter, file)
    if err != nil {
        return
    }
 
    // 关闭表单写入器,以便写入末尾的边界
    writer.Close()
 
    // 创建POST请求,指定URL和请求体
    request, err := http.NewRequest("POST", targetURL, &requestBody)
    if err != nil {
        return
    }
 
    // 设置请求头,指定Content-Type为multipart/form-data
    request.Header.Set("Content-Type", writer.FormDataContentType())
 
    // 发送请求
    client := &http.Client{}
    response, err := client.Do(request)
    if err != nil {
        return
    }
    defer response.Body.Close()
 
    // 读取响应
    body := &bytes.Buffer{}
    _, err = io.Copy(body, response.Body)
    if err != nil {
        return
    }
 
    err = json.NewDecoder(body).Decode(&resp)
    if err != nil {
        return
    }
 
    return
}
 
func Process(audioId uint) (err error) {
    audio, err := models.NewAudioSearch().SetID(audioId).First()
 
    if err != nil {
        return errors.New("查找音频失败")
    }
 
    if audio.AudioStatus != constvar.AudioStatusUploadOk && audio.AudioStatus != constvar.AudioStatusFailed {
        return errors.New("状态不正确")
    }
 
    err = models.NewAudioSearch().SetID(audioId).UpdateByMap(map[string]interface{}{"audio_status": constvar.AudioStatusProcessing})
    if err != nil {
        return errors.New("DB错误")
    }
 
    go func() {
        resp, err := AnalysisAudio(audio.FilePath, conf.AanlysisConf.Url)
        if err != nil {
            logx.Errorf("err when AnalysisAudio:%v", err)
            _ = models.NewAudioSearch().SetID(audioId).UpdateByMap(map[string]interface{}{"audio_status": constvar.AudioStatusFailed})
            return
        }
        if resp.Code != 0 {
            logx.Errorf("AnalysisAudio error return:%v", resp)
            _ = models.NewAudioSearch().SetID(audioId).UpdateByMap(map[string]interface{}{"audio_status": constvar.AudioStatusFailed})
            return
        }
        logx.Infof("AnalysisAudio result: %v", resp)
        words := GetWordFromText(resp.Result, audio)
 
        err = models.WithTransaction(func(db *gorm.DB) error {
            err = models.NewAudioSearch().SetOrm(db).SetID(audioId).UpdateByMap(map[string]interface{}{
                "audio_status": constvar.AudioStatusFinish,
                "score":        resp.Score,
                "tags":         strings.Join(words, ","),
            })
            if err != nil {
                return err
            }
            err = models.NewAudioTextSearch().SetOrm(db).Save(&models.AudioText{
                AudioID:   audio.ID,
                AudioText: resp.Result,
            })
            return err
        })
        if err != nil {
            logx.Infof("AnalysisAudio success but update record failed: %v", err)
            _ = models.NewAudioSearch().SetID(audioId).UpdateByMap(map[string]interface{}{"audio_status": constvar.AudioStatusFailed})
            return
        }
    }()
 
    return nil
}
 
func GetWordFromText(text string, audio *models.Audio) (words []string) {
    if audio == nil {
        return nil
    }
    wordRecords, err := models.NewWordSearch().SetLocomotiveNumber(audio.LocomotiveNumber).FindNotTotal()
    if err != nil || len(wordRecords) == 0 {
        return nil
    }
    for _, v := range wordRecords {
        if strings.Contains(text, v.Content) {
            words = append(words, v.Content)
        }
    }
    return words
}
 
func PreLoad(cxt context.Context) {
    mkdirErr := os.MkdirAll(conf.LocalConf.PreLoadPath, os.ModePerm)
    if mkdirErr != nil {
        logx.Errorf("function os.MkdirAll() err:%v", mkdirErr)
    }
    //文件夹下新增音频文件时触发
    watcher, err := fsnotify.NewWatcher()
    if err != nil {
        log.Fatal(err)
    }
    defer watcher.Close()
    err = watcher.Add(conf.LocalConf.PreLoadPath)
    if err != nil {
        log.Fatal(err)
    }
FOR:
    for {
        select {
        case <-cxt.Done():
            fmt.Println("preload stop")
            break FOR // 退出循环
        case event, ok := <-watcher.Events:
            if !ok {
                continue
            }
            if event.Op&fsnotify.Create == fsnotify.Create {
                // 判断文件类型是否为.mp3或.wav
                if filepath.Ext(event.Name) == ".mp3" || filepath.Ext(event.Name) == ".wav" {
                    time.Sleep(time.Second * 1)
                    //设置文件访问权限
                    err = os.Chmod(event.Name, 0777)
                    if err != nil {
                        logx.Errorf(fmt.Sprintf("%s:%s", event.Name, "设置文件权限失败"))
                    }
                    // 文件名
                    fileName := filepath.Base(event.Name)
                    //校验文件命名
                    arr := strings.Split(fileName, "_")
                    if len(arr) != 6 {
                        logx.Errorf(fmt.Sprintf("%s:%s", fileName, "文件名称错误"))
                        continue
                    }
                    timeStr := arr[4] + strings.Split(arr[5], ".")[0]
                    t, err := time.ParseInLocation("20060102150405", timeStr, time.Local)
                    if err != nil {
                        logx.Errorf(fmt.Sprintf("%s:%s", fileName, "时间格式不对"))
                    }
 
                    //查重
                    _, err = models.NewAudioSearch().SetName(fileName).First()
                    if err != gorm.ErrRecordNotFound {
                        logx.Errorf(fmt.Sprintf("%s:%s", fileName, "重复上传"))
                        continue
                    }
 
                    //将文件移动到uploads文件夹下
                    //判断storePath中末尾是否带
                    var src string
                    if strings.HasSuffix(conf.LocalConf.StorePath, "/") {
                        src = conf.LocalConf.StorePath + fileName
                    } else {
                        src = conf.LocalConf.StorePath + "/" + fileName
                    }
                    err = os.Rename(event.Name, src)
                    if err != nil {
                        logx.Errorf(fmt.Sprintf("%s:%s", fileName, "移动文件失败"))
                        continue
                    }
                    // 读取文件大小
                    fileInfo, err := os.Stat(src)
                    if err != nil {
                        logx.Errorf(fmt.Sprintf("%s:%s", fileName, "获取文件大小失败"))
                        continue
                    }
                    size := fileInfo.Size()
                    fmt.Println("fileName:", fileName, "size:", size, "src1", src)
 
                    audio := &models.Audio{
                        Name:             fileName,
                        Size:             size,
                        FilePath:         src,
                        AudioStatus:      constvar.AudioStatusUploadOk,
                        LocomotiveNumber: arr[0],
                        TrainNumber:      arr[1],
                        DriverNumber:     arr[2],
                        Station:          arr[3],
                        OccurrenceAt:     t,
                        IsFollowed:       0,
                    }
 
                    if err = models.NewAudioSearch().Create(audio); err != nil {
                        logx.Errorf(fmt.Sprintf("%s:%s", fileName, "数据库create失败"))
                        continue
                    }
 
                    go func() {
                        var trainInfoNames = []string{arr[0], arr[1], arr[3]} //
                        var (
                            info   *models.TrainInfo
                            err    error
                            parent models.TrainInfo
                        )
                        for i := 0; i < 3; i++ {
                            name := trainInfoNames[i]
                            class := constvar.Class(i + 1)
                            info, err = models.NewTrainInfoSearch().SetName(name).SetClass(class).First()
                            if err == gorm.ErrRecordNotFound {
                                info = &models.TrainInfo{
                                    Name:     name,
                                    Class:    class,
                                    ParentID: parent.ID,
                                }
                                _ = models.NewTrainInfoSearch().Create(info)
                            }
                            parent = *info
                        }
 
                    }()
                }
            }
        case err, ok := <-watcher.Errors:
            if !ok {
                logx.Errorf(err.Error())
            }
        }
    }
}