- android - 多次调用 OnPrimaryClipChangedListener
- android - 无法更新 RecyclerView 中的 TextView 字段
- android.database.CursorIndexOutOfBoundsException : Index 0 requested, 光标大小为 0
- android - 使用 AppCompat 时,我们是否需要明确指定其 UI 组件(Spinner、EditText)颜色
我正在开发一个机器人,它将接收语音(来自 facebook channel )并将其转换为 .wav。
我认为我在转换过程中遇到了问题。
这是我的代码:
消息 Controller .cs:
namespace SpeechToText.Controllers
{
using System;
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Threading.Tasks;
using System.Web.Http;
using Microsoft.Bot.Connector;
using Services;
using System.Web.Configuration;
using System.IO;
[BotAuthentication]
public class MessagesController : ApiController
{
private readonly MicrosoftCognitiveSpeechService speechService = new MicrosoftCognitiveSpeechService();
/// <summary>
/// POST: api/Messages
/// Receive a message from a user and reply to it
/// </summary>
public async Task<HttpResponseMessage> Post([FromBody]Activity activity)
{
if (activity.Type == ActivityTypes.Message)
{
var connector = new ConnectorClient(new Uri(activity.ServiceUrl));
string message = string.Empty;
IncomingFacebookVoiceMessage voice = null;
try
{
voice = new IncomingFacebookVoiceMessage(activity);
//await SendReply(activity, "The type of your voice file is " + voice.ContentType);
}
catch
{
message = "Send me a voice message instead!"; // no voice file found
}
try
{
if (voice != null)
{
//Download original MP4 voice message
voice.DownloadFile();
var mp4 = voice.GetLocalPathAndFileName();
//Convert MP4 to WAV
// var wavFolder = Utils.GetHomeFolder() + WebConfigurationManager.AppSettings["WAVFilesFolder"];
var wavFolder = "C:" + @"\" + "Teste" ;
var converter = new AudioFileFormatConverter(mp4, wavFolder);
var wav = converter.ConvertMP4ToWAV();
//Convert .WAV file to text
var bing = new MicrosoftCognitiveSpeechService(); //gets the path + filename
// var text = await bing.GetTextFromAudioAsync(wav); //takes path+filename to WAV file, returns text
// convert string to stream
byte[] data = File.ReadAllBytes(wav);
//byte[] byteArray = Encoding.ASCII.GetBytes(contents);
MemoryStream stream = new MemoryStream(data);
var text = await this.speechService.GetTextFromAudioAsync(stream);
if (string.IsNullOrWhiteSpace(text))
{
message = "Looks like you didn't say anything.";
}
else
{
message = text;
}
//Clean up files from disk
voice.RemoveFromDisk();
converter.RemoveTargetFileFromDisk();
}
}
catch (Exception ex)
{
message = "Woah! " + ex.Message.Trim().Trim('.') + "!";
}
Activity reply = activity.CreateReply(message);
await connector.Conversations.ReplyToActivityAsync(reply);
}
else
{
await this.HandleSystemMessage(activity);
}
return Request.CreateResponse(HttpStatusCode.OK);
}
/// <summary>
/// Gets the JwT token of the bot.
/// </summary>
/// <param name="connector"></param>
/// <returns>JwT token of the bot</returns>
/// <summary>
/// Handles the system activity.
/// </summary>
/// <param name="activity">The activity.</param>
/// <returns>Activity</returns>
private async Task<Activity> HandleSystemMessage(Activity activity)
{
switch (activity.Type)
{
case ActivityTypes.DeleteUserData:
// Implement user deletion here
// If we handle user deletion, return a real message
break;
case ActivityTypes.ConversationUpdate:
// Greet the user the first time the bot is added to a conversation.
if (activity.MembersAdded.Any(m => m.Id == activity.Recipient.Id))
{
var connector = new ConnectorClient(new Uri(activity.ServiceUrl));
var response = activity.CreateReply();
response.Text = "Hi! I am SpeechToText Bot. I can understand the content of any audio and convert it to text. Try sending me a wav file.";
await connector.Conversations.ReplyToActivityAsync(response);
}
break;
case ActivityTypes.ContactRelationUpdate:
// Handle add/remove from contact lists
break;
case ActivityTypes.Typing:
// Handle knowing that the user is typing
break;
case ActivityTypes.Ping:
break;
}
return null;
}
}
}
-------------------- IncomingFacebookVoiceMessage.cs
using Microsoft.Bot.Connector;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Net;
using System.Web;
using System.Web.Configuration;
namespace SpeechToText
{
/// <summary>
/// Represents an incoming voice message from facebook messenger,
/// where the voice data is in an MP4 file (the message contains a link to
download it).
/// </summary>
public class IncomingFacebookVoiceMessage
{
#region Properties
/// <summary>
/// URL of the MP4 file sent by user and stored on facebook's servers.
/// </summary>
public Uri MP4FileUrl { get; private set; }
/// <summary>
/// Local filename of the MP4 file after it has been downloaded from Facebook.
/// </summary>
private string MP4LocalFileName { get; set; }
/// <summary>
/// Path to the folder on local disk containing the downloaded voice messages from Facebook.
/// This is configured in Web.config using the FacebookDownloadedVoiceMessagesFolder key.
/// The path in the Web.config will be relative to the site's root folder.
/// </summary>
public string VoiceMessageFolder { get; private set; }
/// <summary>
/// Content-type of the attachment (for debugging - it's not always MP4).
/// </summary>
public string ContentType { get; private set; }
#endregion
#region Constructors
/// <summary>
/// Constructor that uses an MP4 file link that is
/// received in activity.Attachments by bot framework.
/// </summary>
/// <param name="MP4FileUrl">URL of the MP4 file sent by user and stored on facebook's servers.</param>
public IncomingFacebookVoiceMessage(string MP4FileUrl)
{
if (string.IsNullOrWhiteSpace(MP4FileUrl))
{
throw new Exception("The MP4 file URL was empty.");
}
this.MP4FileUrl = new Uri(MP4FileUrl);
this.VoiceMessageFolder = GetVoiceMessagesFolderFromWebConfig();
}
/// <summary>
/// A shortcut constructor that extracts the URL of the MP4 voice message file
/// from the Activity object received by the controller in the Bot Framework.
/// </summary>
/// <param name="activity">The Activity object that contains an attachment of type video/mp4. If no attachment, throws an exception.</param>
public IncomingFacebookVoiceMessage(IMessageActivity activity)
{
var mp4Attachment = activity.Attachments?.FirstOrDefault(a => a.ContentType.Equals("video/mp4") || a.ContentType.Contains("audio") || a.ContentType.Contains("video"));
if (mp4Attachment == null)
{
throw new Exception("The message didn't have a voice attachment.");
}
else
{
this.MP4FileUrl = new Uri(mp4Attachment.ContentUrl);
this.VoiceMessageFolder = GetVoiceMessagesFolderFromWebConfig();
this.ContentType = mp4Attachment.ContentType; //for debugging. Different devices send different content-types, e.g. audio/aac and video/mp4
}
}
#endregion
#region Public methods
/// <summary>
/// Downloads the MP4 file containing the voice message from Facebook.
/// </summary>
/// <returns>The filename (without path) of the MP4 file stored on local disk.</returns>
public string DownloadFile()
{
var filename = GetRandomFileName();
var filenameWithPath = VoiceMessageFolder + @"\" + filename;
//if folder doesn't exist, create it
if (!Directory.Exists(VoiceMessageFolder))
{
Directory.CreateDirectory(VoiceMessageFolder);
}
using (var client = new WebClient())
{
client.DownloadFile(this.MP4FileUrl, filenameWithPath);
}
MP4LocalFileName = filename;
return filename;
}
/// <summary>
/// Removes the downloaded MP4 file from the local disk to clean up space.
/// </summary>
/// <returns>True if successfully removed, false otherwise.</returns>
public bool RemoveFromDisk()
{
try
{
File.Delete(GetLocalPathAndFileName());
return true;
}
catch
{
return false;
}
}
/// <summary>
/// Returns the full local path and filename to the downloaded MP4 voice message.
/// </summary>
/// <returns>E.g. D:\home\site\wwwroot\abc.mp4</returns>
public string GetLocalPathAndFileName()
{
if (string.IsNullOrWhiteSpace(MP4LocalFileName))
{
throw new Exception("The voice message has not been downloaded yet.");
}
return VoiceMessageFolder + @"\" + MP4LocalFileName;
}
#endregion
#region Private methods
/// <summary>
/// Reads Web.config and returns the path to the folder which will store downloaded messages.
/// The folder in the config must be relative to the site's root.
/// </summary>
/// <returns>Full path to the folder that will be used to store MP4 voice messages.</returns>
private string GetVoiceMessagesFolderFromWebConfig()
{
//return Utils.GetHomeFolder() + WebConfigurationManager.AppSettings["FacebookDownloadedVoiceMessagesFolder"];
return "C:" + @"\" + "Teste" ;
}
/// <summary>
/// Generates a random filename using a new GUID.
/// </summary>
/// <returns>A random file name in the format "msg-GUID.mp4".</returns>
private string GetRandomFileName()
{
return "msg-" + Guid.NewGuid() + ".mp4";
}
#endregion
}
}
----------------AudioFileFormatConverter.cs
using MediaToolkit;
using MediaToolkit.Model;
using System;
using System.IO;
using System.Web.Configuration;
namespace SpeechToText
{
/// <summary>
/// Converts audio files between various formats using the open-source
FFMPEG software.
/// </summary>
public class AudioFileFormatConverter
{
#region Properties
/// <summary>
/// Path + filename to the source file.
/// </summary>
public string SourceFileNameAndPath { get; private set; }
/// <summary>
/// Path + filename to the file which is the result of the conversion.
/// </summary>
public string ConvertedFileNameAndPath { get; private set; }
/// <summary>
/// The folder where the converted files will be stored.
/// </summary>
public string TargetPath { get; private set; }
#endregion
#region Constructors
/// <summary>
/// Default constructor.
/// </summary>
/// <param name="sourceFileNameAndPath">Filename and path to the source
file.</param>
/// <param name="targetPath">The folder where the converted files will
be stored.</param>
public AudioFileFormatConverter(string sourceFileNameAndPath, string
targetPath)
{
if (string.IsNullOrWhiteSpace(sourceFileNameAndPath))
{
throw new Exception("Empty source filename.");
}
else if (string.IsNullOrWhiteSpace(targetPath))
{
throw new Exception("Empty target path.");
}
else
{
this.SourceFileNameAndPath = sourceFileNameAndPath;
this.TargetPath = targetPath;
//create folder if it's not there
if (!Directory.Exists(TargetPath))
{
Directory.CreateDirectory(TargetPath);
}
}
}
#endregion
#region Public methods
/// <summary>
/// Converts a source MP4 file to a target WAV file and returns the path
and filename of the converted file.
/// </summary>
/// <returns>The path and filename of the converted file.</returns>
public string ConvertMP4ToWAV()
{
ConvertedFileNameAndPath = TargetPath + @"\" +
Path.GetFileNameWithoutExtension(SourceFileNameAndPath) + ".wav";
//use the same
file name as original, but different folder and extension
var inputFile = new MediaFile { Filename = SourceFileNameAndPath };
var outputFile = new MediaFile { Filename = ConvertedFileNameAndPath
};
using (var engine = new Engine(GetFFMPEGBinaryPath()))
{
engine.Convert(inputFile, outputFile);
}
return ConvertedFileNameAndPath;
}
/// <summary>
/// Removes the converted file from disk to free up space.
/// Doesn't remove the source file.
/// </summary>
/// <returns>True if file deleted successfully, false if cannot delete,
exception if filename is empty.</returns>
public bool RemoveTargetFileFromDisk()
{
if (string.IsNullOrWhiteSpace(ConvertedFileNameAndPath))
{
throw new Exception("The file has not been converted yet, so it cannot be deleted.");
}
try
{
File.Delete(ConvertedFileNameAndPath);
return true;
}
catch
{
return false;
}
}
#endregion
#region Private methods
/// <summary>
/// Reads the config to determine the location of ffmpeg.exe.
/// </summary>
/// <returns>The full path and filename to the ffmpeg.exe program.
</returns>
public string GetFFMPEGBinaryPath()
{
// return Utils.GetHomeFolder() +
WebConfigurationManager.AppSettings["FFMPEGBinaryLocation"];
return "D:" + @"\" + "Teste";
}
#endregion
}
}
这是我的输出信息:
你能帮我解决这个问题吗...我应该在 Facebook Messenger 中的什么地方存储音频寄存器?
最佳答案
我认为您只缺少 GetFFMPEGBinaryPath() 方法中的 ffmpeg.exe。一旦我将它添加到路径的末尾,您的代码就会按预期运行。
public string GetFFMPEGBinaryPath()
{
return "D:" + @"\Teste\ffmpeg-20170921-183fd30-win64-static\bin\ffmpeg.exe";
}
关于c# - 如何将语音转换为 .wav 格式并使用它,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46448218/
我有一个说一些短语的音板应用程序,但是现在我希望能够从男声/女声中改变出来,问题是我不知道该怎么做。任何帮助,将不胜感激。 我正在使用AVFoundation/AVAudioPlayer播放声音。 谢
关闭。这个问题不符合Stack Overflow guidelines .它目前不接受答案。 我们不允许提问寻求书籍、工具、软件库等的推荐。您可以编辑问题,以便用事实和引用来回答。 关闭 4 年前。
因为我想在后台录制音频,所以我使用了服务..但是我无法在服务中录制音频。 我在 Activity 中尝试了相同的代码,它对我有用。但是如何在输入语音/语音时在后台进行录音,这意味着如果有语音输入就应该
关闭。这个问题不符合Stack Overflow guidelines .它目前不接受答案。 我们不允许提问寻求书籍、工具、软件库等的推荐。您可以编辑问题,以便用事实和引用来回答。 关闭 6 年前。
我有一个音频流,我会从中提取单词(语音)。因此,例如使用 audio.wav 我会得到 001.wav、002.wav、003.wav 等,其中每个 XXX.wav 是一个词。 我正在寻找一个库或程序
不幸的是,我只能说四种语言,那么如果我知道文本的语言,我如何知道我必须使用哪种 OS X 语音?我在Apple的文档中找不到任何有关它的信息。至少有一张有语音/语言的 table 吗? 最佳答案 您可
有没有办法从命令行使用 MS Speech 实用程序?我可以在 Mac 上完成,但在 Windows XP 上找不到任何引用。 最佳答案 我在这个主题上的 2 美分,命令行单行: 在 Win 上使用
所以我开始了我的不和谐机器人的音乐部分。现在,正如我在上一个问题中所做的那样,这里只是音乐命令的片段:Pastebin #1 在显示 if (msg.member.voiceConnection ==
有谁知道有什么好的 API 或库可以听(语音)文本。我尝试听三种语言的(语音)文本,我想知道最好从哪里开始以及如何开始。我可以对所有三种语言使用通用语音吗?我将使用 eclipse 和 java 作为
首先,我只是一个爱好者,如果这是一个愚蠢的问题或者我太天真了,我很抱歉。 (这也意味着我买不起昂贵的库) 情况是这样的:我正在使用 C#.NET 构建一个简单的语音聊天应用程序(类似于 Ventril
我正在制作一个模块,可以生成和传输语音 IP 数据包。我知道我必须为服务类型字段设置一些值。这个值是多少? 最佳答案 该值应该是x。 关于c - 语音 ip 的服务类型字段集,我们在Stack Ove
有人能帮帮我吗?我使用 SAPI 语音文本,但我不能设置女声,这是代码,它用男声说话,但我想改变它,我想要女声 #include "stdafx.h" using namespace std; voi
我正在寻找一种方法来为一个项目在 Java 中识别预注册的语音命令,但我还没有想出一个好的方法,我研究了快速傅里叶 和处理 wave 文件 的不同方法,但我无法决定我应该如何实现它。 这个想法很简单,
我在 android 的语音识别 API 工作。 我是 Speech Recognition Api 的新手,我的要求是西类牙语语音,并从 Android 的语音识别 API 中获得西类牙语的最佳匹配
我在 Java 中使用一组名为(MaryTTS[实际上还有更多])的库来将 text to speech 转换为该目的,使用以下代码: public class TextToSpeech {
我正在尝试使用webRTC和php作为服务器端来实现单向语音传输。 查看samples ,我无法理解webRTC机制。 在我看来,流程应该是这样的: 调用者和接收者在服务器上注册 接收者监听来电 调用
我的名字是 Joey,我想知道是否有一种在 C++ 中使用语音的方法,如果有人可以给我指出引用资料和书籍,非常感谢...... 最佳答案 你应该看看 Windows Text-To-Speech AP
关闭。这个问题需要更多focused .它目前不接受答案。 想改进这个问题吗? 更新问题,使其只关注一个问题 editing this post . 关闭 8 年前。 Improve this qu
我正在使用 Java 语音识别 API - Jarvis,位于 https://github.com/lkuza2/java-speech-api 但是,当我运行应用程序时,出现错误:服务器返回 HT
我们正在做一个需要讲阿拉伯语的项目,我们找到了一个开源工具,Mbrola project , 可以做到这一点。 但是,我还需要一些方法将阿拉伯语文本转换为 SAMPA 语音。那么有人可以帮助我将阿拉伯
我是一名优秀的程序员,十分优秀!