| | |
| | | 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.model.dto.PsdkModelDto; |
| | | import com.dji.sample.speak.model.dto.SpeakVoiceFileDto; |
| | | import com.dji.sample.speak.model.dto.SpeakVoiceStartDto; |
| | | import com.dji.sample.speak.model.dto.SpeakVolumeDto; |
| | | 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.PsdkModelEnum; |
| | | import com.dji.sample.speak.model.enums.VoiceEnums; |
| | | import com.dji.sample.speak.model.param.SpeakVoiceStartParam; |
| | | import com.dji.sample.speak.service.SpeakVoiceService; |
| | | import com.dji.sample.speak.util.MD5Util; |
| | | import lombok.extern.log4j.Log4j; |
| | | 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.File; |
| | | import java.io.IOException; |
| | | import java.time.LocalDateTime; |
| | | import java.time.format.DateTimeFormatter; |
| | | import java.util.List; |
| | | import java.util.stream.Collectors; |
| | | |
| | | @Service |
| | | |
| | |
| | | private IMessageSenderService messageSenderService; |
| | | @Autowired |
| | | private MinioPojo pojo; |
| | | @Autowired |
| | | private SpeakVoiceMapper voiceMapper; |
| | | |
| | | @Override |
| | | public int takeVoice( String sn , Integer psdk_index, String name, File file) { |
| | | 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); |
| | | fileDto.setMd5(MD5Util.getMD5Checksum(file)); |
| | | 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) { |
| | | return messageSenderService.publishServicesTopic(sn, VoiceEnums.SPEAKER_REPLAY.getMethod(), psdk_index).getResult(); |
| | | 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) { |
| | | return messageSenderService.publishServicesTopic(sn,VoiceEnums.SPEAKER_PLAY_STOP.getMethod(), psdk_index).getResult(); |
| | | 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(); |
| | | 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(); |
| | | return messageSenderService.publishServicesTopic(sn, VoiceEnums.SPEAKER_PLAY_VOLUME_SET.getMethod(), dto).getResult(); |
| | | } |
| | | |
| | | @Override |
| | | public int awayRiver(int id) { |
| | | SpeakVoiceEntity voiceEntity=voiceMapper.selectById(id); |
| | | SpeakVoiceStartDto dto = new SpeakVoiceStartDto(); |
| | | SpeakVoiceFileDto fileDto = new SpeakVoiceFileDto(); |
| | | fileDto.setUrl(voiceEntity.getUrl()); |
| | | fileDto.setMd5(voiceEntity.getMd5()); |
| | | fileDto.setFormat(FormatEnum.PCM); |
| | | fileDto.setName(voiceEntity.getName()); |
| | | dto.setPsdk_index(2); |
| | | dto.setFile(fileDto); |
| | | return messageSenderService.publishServicesTopic("4TADKCM0010016", 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) { |
| | |
| | | // 格式化时间,生成当前时间 |
| | | return currentTime.format(DateTimeFormatter.ofPattern("MMddHHmm")); |
| | | } |
| | | |
| | | public static double getAudioDuration(File file) throws UnsupportedAudioFileException, IOException { |
| | | 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)); |
| | | } |
| | | } |