aix
2024-08-08 39ffdf5754b4ebca682bd3c925f7a500e8a1bbda
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
package com.dji.sample.speak.service.serviceImpl;
 
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.dji.sample.common.model.Pagination;
import com.dji.sample.common.model.PaginationData;
import com.dji.sample.component.mqtt.service.IMessageSenderService;
import com.dji.sample.media.model.MinioPojo;
import com.dji.sample.media.service.impl.FileServiceImpl;
import com.dji.sample.speak.dao.SpeakVoiceMapper;
import com.dji.sample.speak.model.dto.*;
import com.dji.sample.speak.model.entity.SpeakVoiceEntity;
import com.dji.sample.speak.model.enums.FormatEnum;
import com.dji.sample.speak.model.enums.VoiceEnums;
import com.dji.sample.speak.service.SpeakVoiceService;
import com.dji.sample.speak.util.MD5Util;
import com.dji.sample.territory.service.impl.TbFjServiceImpl;
import io.minio.MinioClient;
import io.minio.PutObjectArgs;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
 
import javax.sound.sampled.AudioFormat;
import javax.sound.sampled.AudioInputStream;
import javax.sound.sampled.AudioSystem;
import javax.sound.sampled.UnsupportedAudioFileException;
import java.io.*;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.List;
import java.util.stream.Collectors;
 
@Service
 
public class SpeakVoiceServiceImpl implements SpeakVoiceService {
    @Autowired
    private IMessageSenderService messageSenderService;
    @Autowired
    private MinioPojo pojo;
    @Autowired
    private SpeakVoiceMapper voiceMapper;
 
    @Override
    public int takeVoice(String sn, Integer psdk_index, String name, File file,Integer volumn) throws UnsupportedAudioFileException, IOException {
        SpeakVoiceEntity voiceEntity = new SpeakVoiceEntity();
        SpeakVoiceStartDto dto = new SpeakVoiceStartDto();
        SpeakVoiceFileDto fileDto = new SpeakVoiceFileDto();
        SpeakVolumeDto volumeDto=new SpeakVolumeDto();
        volumeDto.setPsdk_index(psdk_index);
        volumeDto.setPlay_volume(volumn);
        String url = upMinio(file, name);
        fileDto.setUrl(url);
        String md5 = MD5Util.getMD5Checksum(file);
        fileDto.setMd5(md5);
        fileDto.setFormat(FormatEnum.PCM);
        fileDto.setName(name);
        dto.setPsdk_index(psdk_index);
        dto.setFile(fileDto);
        double time = getAudioDuration(file);
        voiceEntity.setMd5(md5);
        voiceEntity.setSecond(time);
        voiceEntity.setUrl(url);
        voiceEntity.setName(name);
        voiceMapper.insert(voiceEntity);
        messageSenderService.publishServicesTopic(sn, VoiceEnums.SPEAKER_PLAY_VOLUME_SET.getMethod(), volumeDto);
        return messageSenderService.publishServicesTopic(sn, VoiceEnums.SPEAKER_AUDIO_PLAY_START.getMethod(), dto).getResult();
    }
 
    @Override
    public int restartVoice(String sn, Integer psdk_index) {
        SpeakRestartDto dto = new SpeakRestartDto();
        dto.setPsdk_index(psdk_index);
        return messageSenderService.publishServicesTopic(sn, VoiceEnums.SPEAKER_REPLAY.getMethod(), dto).getResult();
    }
 
    @Override
    public int stopVoice(String sn, Integer psdk_index) {
        SpeakStopDto dto = new SpeakStopDto();
        dto.setPsdk_index(psdk_index);
        return messageSenderService.publishServicesTopic(sn, VoiceEnums.SPEAKER_PLAY_STOP.getMethod(), dto).getResult();
    }
 
    @Override
    public int setVoiceMode(String sn, PsdkModelDto dto) {
        return messageSenderService.publishServicesTopic(sn, VoiceEnums.SPEAKER_PLAY_MODE_SET.getMethod(), dto).getResult();
    }
 
    @Override
    public int setVoiceVolume(SpeakVolumeDto dto, String sn) {
        return messageSenderService.publishServicesTopic(sn, VoiceEnums.SPEAKER_PLAY_VOLUME_SET.getMethod(), dto).getResult();
    }
 
    @Override
    public int awayRiver(String sn) {
        SpeakVoiceStartDto dto = new SpeakVoiceStartDto();
        SpeakVoiceFileDto fileDto = new SpeakVoiceFileDto();
        fileDto.setUrl("http://dev.jxpskj.com:9000/cloud-bucket/请远离河道07151604");
        fileDto.setMd5("d2b448dcba09071834d02f082dc5386f");
        fileDto.setFormat(FormatEnum.PCM);
        fileDto.setName("河道危险");
        dto.setPsdk_index(2);
        dto.setFile(fileDto);
        return messageSenderService.publishServicesTopic(sn, VoiceEnums.SPEAKER_AUDIO_PLAY_START.getMethod(), dto).getResult();
    }
 
    @Override
    public PaginationData<SpeakVoiceEntity> getVoices(Integer pages, Integer page_size) {
        // Paging Query
        Page<SpeakVoiceEntity> page = voiceMapper.selectPage(
                new Page<>(pages, page_size),
                new LambdaQueryWrapper<SpeakVoiceEntity>()
        );
        // Wrap the results of a paging query into a custom paging object.
        List<SpeakVoiceEntity> records = page.getRecords()
                .stream()
                .collect(Collectors.toList());
 
        return new PaginationData<>(records, new Pagination(page));
    }
 
    public String upMinio(File file, String fileName) {
        String endpoint = pojo.getEndpoint();
        String accessKey = pojo.getAccessKey();
        String secretKey = pojo.getSecretKey();
        String bucketName = pojo.getBucket();
        String objectName = "/" + fileName + getNowTimeName();
        FileServiceImpl.uploadFile(endpoint, accessKey, secretKey, bucketName, objectName, file, "audio/wav");
        return endpoint + "/" + bucketName + objectName;
    }
 
 
    public static String getNowTimeName() {
        LocalDateTime currentTime = LocalDateTime.now();
        // 格式化时间,生成当前时间
        return currentTime.format(DateTimeFormatter.ofPattern("MMddHHmm"));
    }
 
    public static double getAudioDuration(File file) {
        try {
            AudioInputStream audioInputStream = AudioSystem.getAudioInputStream(file);
            AudioFormat format = audioInputStream.getFormat();
            long audioFileLength = file.length();
            int frameSize = format.getFrameSize();
            float frameRate = format.getFrameRate();
            return Math.round(audioFileLength / (frameSize * frameRate));
        } catch (Exception e) {
            return 0.0;
        }
    }
 
    public int takeVoices(String sn, Integer psdk_index, String name, String url) {
        SpeakVoiceEntity voiceEntity = new SpeakVoiceEntity();
        SpeakVoiceStartDto dto = new SpeakVoiceStartDto();
        SpeakVoiceFileDto fileDto = new SpeakVoiceFileDto();
        SpeakVolumeDto volumeDto = new SpeakVolumeDto();
        File file = TbFjServiceImpl.downloadFile(url);
        String md5 = MD5Util.getMD5Checksum(file);
        volumeDto.setPsdk_index(psdk_index);
        volumeDto.setPlay_volume(100);
        String names = name + ".wav";
        fileDto.setUrl(url);
        fileDto.setMd5(md5);
        fileDto.setFormat(FormatEnum.PCM);
        fileDto.setName(name);
        dto.setPsdk_index(psdk_index);
        dto.setFile(fileDto);
        voiceEntity.setMd5(md5);
        voiceEntity.setUrl(url);
        voiceEntity.setName(names);
        voiceMapper.insert(voiceEntity);
        messageSenderService.publishServicesTopic(sn, VoiceEnums.SPEAKER_PLAY_VOLUME_SET.getMethod(), volumeDto);
        return messageSenderService.publishServicesTopic(sn, VoiceEnums.SPEAKER_AUDIO_PLAY_START.getMethod(), dto).getResult();
    }
 
    public void saveFileToLocal(File file, String destinationPath) throws IOException {
        // 创建目标文件对象
        File destinationFile = new File(destinationPath);
 
        // 确保目标文件的父目录存在
        destinationFile.getParentFile().mkdirs();
 
        // 将文件内容写入目标文件
        try (FileOutputStream outputStream = new FileOutputStream(destinationFile);
             InputStream inputStream = new FileInputStream(file)) {
            byte[] buffer = new byte[1024];
            int bytesRead;
            while ((bytesRead = inputStream.read(buffer)) != -1) {
                outputStream.write(buffer, 0, bytesRead);
            }
        }
    }
}