public JsonBehavior GetJson(Opus doc, Snippet chapter, Snippet snippet)
 {
     if (snippet is Section)
     {
         var numberChain = doc.ShowNumberChain
                     ? snippet.GetSectionNumber(chapter.Id)
                     : snippet.GetSectionLevel(chapter.Id);
         return(new SectionJsonBehavior().GetJson(doc, chapter, snippet as Section, numberChain));
     }
     if (snippet is ImageSnippet)
     {
         return(new ImageJsonBehavior().GetJson(doc, chapter, snippet));
     }
     if (snippet is SidebarSnippet)
     {
         return(new SidebarJsonBehavior().GetJson(doc, chapter, snippet));
     }
     if (snippet is TextSnippet)
     {
         return(new TextJsonBehavior().GetJson(doc, chapter, snippet));
     }
     if (snippet is ListingSnippet)
     {
         return(new ListingJsonBehavior().GetJson(doc, chapter, snippet));
     }
     if (snippet is TableSnippet)
     {
         return(new TableJsonBehavior().GetJson(doc, chapter, snippet));
     }
     return(null);
 }
Exemplo n.º 2
0
        /// <summary> Creates a new Opus encoder. </summary>
        /// <param name="samplingRate">Sampling rate of the input signal (Hz). Supported Values:  8000, 12000, 16000, 24000, or 48000</param>
        /// <param name="channels">Number of channels in input signal. Supported Values: 1 or 2</param>
        /// <param name="frameLength">Length, in milliseconds, that each frame takes. Supported Values: 2.5, 5, 10, 20, 40, 60</param>
        /// <param name="bitrate">Bitrate (kbit/s) used for this encoder. Supported Values: 1-512. Null will use the recommended bitrate. </param>
        /// <param name="application">Coding mode.</param>
        public OpusEncoder(int samplingRate, int channels, int frameLength, int?bitrate, OpusApplication application)
            : base(samplingRate, channels, frameLength)
        {
            if (bitrate != null && (bitrate < 1 || bitrate > MAX_BITRATE))
            {
                throw new ArgumentOutOfRangeException(nameof(bitrate));
            }

            BitRate     = bitrate;
            Application = application;

            OpusError error;

            _ptr = Opus.CreateEncoder(samplingRate, channels, (int)application, out error);

            if (error != OpusError.OK)
            {
                throw new InvalidOperationException($"Error occured while creating encoder: {error}");
            }

            SetForwardErrorCorrection(true);

            if (bitrate != null)
            {
                SetBitrate(bitrate.Value);
            }
        }
Exemplo n.º 3
0
        public static Opus ImportSingleHtml(byte[] content, Import mapping, string name)
        {
            Opus opus = new Opus();
            var  html = Encoding.UTF8.GetString(content);

            try {
                // convert prepared HTML into internal <Content> XML (backup and restore format)
                var parameters = new System.Collections.Specialized.NameValueCollection();
                if (mapping != null)
                {
                    mapping.CharacterStyles.ForEach(c => parameters.Add(c.Key, c.Value));
                    mapping.ParagraphStyles.ForEach(c => parameters.Add(c.Key, c.Value));
                    mapping.NumberingStyles.ForEach(c => parameters.Add(c.Key, c.Value));
                }
                var xml = Html2XmlUtil.HtmlToOpusXsltParser(html, parameters);
                using (var xmlStream = new MemoryStream(Encoding.UTF8.GetBytes(xml))) {
                    var xDoc = XDocument.Load(xmlStream);
                    // use restore to create import, all content is in Opus, then
                    RestoreOpusFromFile(opus, xDoc, null);
                    // register the import information with the uploaded and converted HTML
                }
            }
            catch (Exception ex) {
            }
            return(opus);
        }
Exemplo n.º 4
0
            void SourceFiles_UpdateOptions2(Opus.Core.IModule module, Opus.Core.Target target)
            {
                C.ICCompilerOptions compilerOptions = module.Options as C.ICCompilerOptions;
                compilerOptions.WarningsAsErrors = false;
                compilerOptions.IncludePaths.Include(this, "src", "src");

                // VS2010 has this, but nothing else
                if (Opus.Core.State.PackageInfo["VisualC"].Version != "10.0")
                {
                    compilerOptions.Defines.Add("STATUS_INVALID_PARAMETER=0xC000000DL");
                }

                C.ICxxCompilerOptions cxxCompilerOptions = module.Options as C.ICxxCompilerOptions;
                cxxCompilerOptions.ExceptionHandler = C.Cxx.EExceptionHandler.Asynchronous;

                #if true
                #else
                C.IToolchainOptions toolchainOptions = compilerOptions.ToolchainOptionCollection as C.IToolchainOptions;
                toolchainOptions.CharacterSet = C.ECharacterSet.Unicode;
                #endif
                // TODO: not sure why these are not passed through
                if (Opus.Core.State.PackageInfo["VisualC"].Version == "10.0")
                {
                    compilerOptions.Defines.Add("UNICODE");
                    compilerOptions.Defines.Add("_UNICODE");
                }
            }
Exemplo n.º 5
0
        /// <summary>
        /// Disconnects and disposes this voice connection.
        /// </summary>
        public void Dispose()
        {
            if (IsDisposed)
            {
                return;
            }

            IsDisposed    = true;
            IsInitialized = false;
            TokenSource.Cancel();
#if !NETSTANDARD1_1
            if (Configuration.EnableIncoming)
            {
                ReceiverTokenSource.Cancel();
            }
#endif

            try
            {
                VoiceWs.DisconnectAsync(null).ConfigureAwait(false).GetAwaiter().GetResult();
                UdpClient.Close();
            }
            catch (Exception)
            { }

            Opus?.Dispose();
            Opus   = null;
            Sodium = null;
            Rtp    = null;

            if (VoiceDisconnected != null)
            {
                VoiceDisconnected(Guild);
            }
        }
Exemplo n.º 6
0
        /// <summary>
        /// Encodes, encrypts, and sends the provided PCM data to the connected voice channel.
        /// </summary>
        /// <param name="pcmData">PCM data to encode, encrypt, and send.</param>
        /// <param name="blockSize">Millisecond length of the PCM data.</param>
        /// <param name="bitRate">Bitrate of the PCM data.</param>
        /// <returns>Task representing the sending operation.</returns>
        public async Task SendAsync(byte[] pcmData, int blockSize, int bitRate = 16)
        {
            if (!IsInitialized)
            {
                throw new InvalidOperationException("The connection is not initialized");
            }

            await PlaybackSemaphore.WaitAsync().ConfigureAwait(false);

            var rtp = Rtp.Encode(Sequence, Timestamp, SSRC);

            var dat = Opus.Encode(pcmData, 0, pcmData.Length, bitRate);

            dat = Sodium.Encode(dat, Rtp.MakeNonce(rtp), Key);
            dat = Rtp.Encode(rtp, dat);

            await SendSpeakingAsync(true).ConfigureAwait(false);

            // works:
            //await UdpClient.SendAsync(dat, dat.Length).ConfigureAwait(false);
            await UdpClient.SendNativelyAsync(dat, dat.Length).ConfigureAwait(false);

            Sequence++;
            Timestamp += 48 * (uint)blockSize;

            PlaybackSemaphore.Release();
        }
Exemplo n.º 7
0
        // ReSharper restore InconsistentNaming
        #endregion

        #region Override, Properties

        #endregion

        #region Overriden Mthods

        public override JsonBehavior GetJson(Opus doc, Snippet currentChapter, Snippet i)
        {
            var snippet = i as ImageSnippet;

            //var rawContent = String.Format("<div class=\"img\" style='width:{1};'><img src='{0}?{2}' /></div>", Url.Action("GetImage", new { id = Model.Id }),'+
            //    '(String.IsNullOrEmpty(Model.Properties) ? 300 : Json.Decode<ImageProperties>(Model.Properties).ImageWidth), '+
            //    '(String.IsNullOrEmpty(Model.Properties) ? 300 : Json.Decode<ImageProperties>(Model.Properties).ImageHeight),'+
            //    'DateTime.Now.Ticks);
            return(new ImageJsonBehavior {
                documentId = doc.Id,
                chapterId = currentChapter.Id,
                snippetId = snippet.Id,
                width = snippet.Width,
                height = snippet.Height,
                imageUrl = snippet.ItemHref,
                title = snippet.Title,
                widgetName = snippet.WidgetName,
                imageLocalization = "Figure",
                orderNr = snippet.OrderNr,
                levelId = snippet.Level,
                parentId = snippet.Parent.Id,
                isReadOnly = snippet.ReadOnly,
                originalheight = snippet.ImageProperties.OriginalHeight,
                originalwidth = snippet.ImageProperties.OriginalWidth,
                properties = snippet.Properties
            });
        }
Exemplo n.º 8
0
        private Section CreateChapter(Opus parent, string name, bool isBoilerplate = false)
        {
            var s = new Section {
                Content       = Encoding.UTF8.GetBytes("Kapitel " + name),
                Name          = "Kapitel " + name,
                Parent        = parent,
                IsBoilerplate = isBoilerplate,
                OrderNr       = parent.Children.Any() ? parent.Children.Max(c => c.OrderNr) + 1 : 0,
                LocaleId      = parent.LocaleId,
                Children      = new List <Element>(new[] {
                    new TextSnippet {
                        Content  = Encoding.UTF8.GetBytes(GetLorem()),
                        Name     = "Text 1 " + name,
                        OrderNr  = 1,
                        Children = new List <Element>(new [] { new SidebarSnippet {
                                                                   Name    = "Inner Sidebar",
                                                                   Content = Encoding.UTF8.GetBytes(GetLorem())
                                                               } })
                    },
                    new TextSnippet {
                        Content = Encoding.UTF8.GetBytes(GetLorem()),
                        Name    = "Text 2 " + name,
                        OrderNr = 2
                    },
                    new TextSnippet {
                        Content = Encoding.UTF8.GetBytes(GetLorem()),
                        Name    = "Text 3 " + name,
                        OrderNr = 3
                    }
                })
            };

            parent.Children.Add(s);
            return(s);
        }
Exemplo n.º 9
0
        // ReSharper restore InconsistentNaming

        #endregion

        #region Override, Properties

        #endregion

        #region Overriden Methods

        public override JsonBehavior GetJson(Opus doc, Snippet currentChapter, Snippet i)
        {
            var snippet = i as TableSnippet;

            return(new TableJsonBehavior {
                documentId = doc.Id,
                chapterId = currentChapter.Id,
                snippetId = snippet.Id,
                content = snippet.RawContent,
                genericChapterNumber = doc.ShowNumberChain ? currentChapter.OrderNr.ToString() : "#",
                tableLocalization = "Table",
                tableNumber = 0,
                rows = snippet.Rows,
                columns = snippet.Cols,
                defaultContent = snippet.DefaultContent,
                repeatHeadRow = snippet.RepeatHeadRow,
                title = snippet.Title,
                widgetName = snippet.WidgetName,
                levelId = snippet.Level,
                parentId = snippet.Parent.Id,
                orderNr = snippet.OrderNr,
                snippetCounter = currentChapter.Children.FlattenHierarchy().OfType <TableSnippet>().ToList().IndexOf(snippet) + 1,
                isReadOnly = snippet.ReadOnly
            });
        }
Exemplo n.º 10
0
// ReSharper restore InconsistentNaming

        #endregion

        #region Override, Properties

        #endregion

        #region Overriden Methods

        public override JsonBehavior GetJson(Opus doc, Snippet currentChapter, Snippet i)
        {
            var snippet = i as ImageSnippet;

            return(new ImageJsonBehavior
            {
                documentId = doc.Id,
                chapterId = currentChapter.Id,
                snippetId = snippet.Id,
                width = snippet.Width,
                height = snippet.Height,
                imageUrl = snippet.ItemHref,
                title = snippet.Title,
                widgetName = snippet.WidgetName,
                imageLocalization = "Figure",
                orderNr = snippet.OrderNr,
                genericChapterNumber = doc.ShowNumberChain ? currentChapter.OrderNr.ToString() : "#",
                snippetCounter = currentChapter.Children.FlattenHierarchy().OfType <ImageSnippet>().ToList().IndexOf(snippet) + 1,
                levelId = snippet.Level,
                parentId = snippet.Parent.Id,
                isReadOnly = snippet.ReadOnly,
                originalheight = snippet.ImageProperties.OriginalHeight,
                originalwidth = snippet.ImageProperties.OriginalWidth,
                mimetype = snippet.MimeType,
                properties = snippet.Properties
            });
        }
Exemplo n.º 11
0
        /// <summary>
        ///     Determines the correct codec to use for a stream header
        ///     packet.
        /// </summary>
        /// <param name="packet">
        ///     A <see cref="ByteVector" /> object containing the first
        ///     packet of an Ogg logical bitstream.
        /// </param>
        /// <returns>
        ///     A <see cref="Codec" /> object capable of handling
        ///     <paramref name="packet" /> and subsequent packets from
        ///     the same stream.
        /// </returns>
        /// <exception cref="UnsupportedFormatException">
        ///     No registered codec capable of processing
        ///     <paramref
        ///         name="packet" />
        ///     could be found.
        /// </exception>
        /// <remarks>
        ///     This method will first use <see cref="CodecProvider" />
        ///     delegates registered with <see cref="AddCodecProvider" />
        ///     and then attempt to use the built-in codecs.
        /// </remarks>
        public static Codec GetCodec(ByteVector packet)
        {
            Codec c = null;

            foreach (var p in providers)
            {
                c = p(packet);
                if (c != null)
                {
                    return(c);
                }
            }

            c = Vorbis.FromPacket(packet);
            if (c != null)
            {
                return(c);
            }

            c = Theora.FromPacket(packet);
            if (c != null)
            {
                return(c);
            }

            c = Opus.FromPacket(packet);
            if (c != null)
            {
                return(c);
            }

            throw new UnsupportedFormatException("Unknown codec.");
        }
Exemplo n.º 12
0
 private void DoImports(int id, Import importModule, Opus opus, IEnumerable <ResourceFile> docxRes, bool importOverwrite)
 {
     try {
         // import all files in one single step
         foreach (byte[] docx in docxRes.Select(item => BlobFactory.GetBlobStorage(item.ResourceId, BlobFactory.Container.Resources)).Select(b => b.Content))
         {
             // import
             importModule.CreateHtmlFragments(docx);
         }
         // once done store fragments
         if (importModule.Fragments.Any())
         {
             // get the Opus we add the content to
             var opusElement = Ctx.Elements.OfType <Opus>().FirstOrDefault(e => e.Project.Id == id && e.Id == opus.Id);
             if (importOverwrite && opusElement.HasChildren())
             {
                 opusElement.Children.ToList().ForEach(e => Ctx.Elements.Remove(e));
                 Ctx.SaveChanges();
             }
             ReparentFragments(importModule.Fragments, opusElement);
         }
     } catch (Exception ex) {
         // handle error
         Console.ForegroundColor = ConsoleColor.Red;
         Console.WriteLine(ex.Message);
         Console.ForegroundColor = ConsoleColor.White;
     }
 }
Exemplo n.º 13
0
            void Source_UpdateOptions(Opus.Core.IModule module, Opus.Core.Target target)
            {
                C.ICCompilerOptions compilerOptions = module.Options as C.ICCompilerOptions;
                compilerOptions.IgnoreStandardIncludePaths = false;

                C.ICxxCompilerOptions cxxCompilerOptions = module.Options as C.ICxxCompilerOptions;
                cxxCompilerOptions.ExceptionHandler = C.Cxx.EExceptionHandler.Asynchronous;
            }
Exemplo n.º 14
0
 protected override void Dispose(bool disposing)
 {
     if (_ptr != IntPtr.Zero)
     {
         Opus.DestroyEncoder(_ptr);
         _ptr = IntPtr.Zero;
     }
 }
Exemplo n.º 15
0
        public ClientUser(int userID, string name)
        {
            UserID = userID;
            Name   = name;

            VoiceCodec  = new Opus();
            audioPlayer = Client.CreateAudioPlayer();
        }
Exemplo n.º 16
0
        /// <summary> Gets or sets whether Forward Error Correction is enabled. </summary>
        public void SetForwardErrorCorrection(bool value)
        {
            int result = Opus.EncoderCtl(_ptr, OpusCtl.SetInbandFECRequest, value ? 1 : 0);

            if (result < 0)
            {
                throw new Exception(((OpusError)result).ToString());
            }
        }
Exemplo n.º 17
0
        protected override void Seed(MyContext context)
        {
            base.Seed(context);
            Console.WriteLine("Seeding Database");
            // Project
            var p = new Project {
                Name   = "Test",
                Short  = "Test for Moving / Copying",
                Active = true
            };

            Console.WriteLine("Add Project");
            context.Projects.Add(p);
            Console.WriteLine("Saving");
            context.SaveChanges();
            Console.WriteLine("Saved");

            Console.WriteLine("Create Opus");
            // Opus with boilerplates
            var ob = new Opus {
                Name      = "Boilerplates",
                Active    = true,
                LocaleId  = "de",
                Variation = VariationType.HeadRevision,
                Version   = 1,
                Project   = p
            };

            context.Elements.Add(ob);
            var s = CreateChapter(ob, "Boilerplate", true);

            Console.WriteLine("Chapter " + s.Name + " created");

            // Opus with regular content
            var oc = new Opus {
                Name      = "Test Manual",
                Active    = true,
                LocaleId  = "de",
                Variation = VariationType.HeadRevision,
                Version   = 1,
                Project   = p
            };

            context.Elements.Add(oc);
            var r = CreateChapter(oc, "Regular Text");

            Console.WriteLine("Chapter " + r.Name + " created");
            r = CreateChapter(oc, "Second Text");
            Console.WriteLine("Chapter " + r.Name + " created");

            Console.WriteLine("Saving");
            context.SaveChanges();
            Console.WriteLine("Saved");
            // Content, two chapters with 2 text snippets each

            Console.WriteLine("Exiting Seed...");
        }
Exemplo n.º 18
0
        /// <summary> Gets or sets whether Forward Error Correction is enabled. </summary>
        public void SetBitrate(int value)
        {
            int result = Opus.EncoderCtl(_ptr, OpusCtl.SetBitrateRequest, value * 1000);

            if (result < 0)
            {
                throw new Exception(((OpusError)result).ToString());
            }
        }
Exemplo n.º 19
0
        /// <summary>
        /// Encodes, encrypts, and sends the provided PCM data to the connected voice channel.
        /// </summary>
        /// <param name="pcmData">PCM data to encode, encrypt, and send.</param>
        /// <param name="blockSize">Millisecond length of the PCM data.</param>
        /// <param name="bitRate">Bitrate of the PCM data.</param>
        /// <returns>Task representing the sending operation.</returns>
        public async Task SendAsync(byte[] pcmData, int blockSize, int bitRate = 16)
        {
            if (!IsInitialized)
            {
                throw new InvalidOperationException("The connection is not initialized");
            }

            await PlaybackSemaphore.WaitAsync().ConfigureAwait(false);

            var rtp = Rtp.Encode(Sequence, Timestamp, SSRC);

            var dat = Opus.Encode(pcmData, 0, pcmData.Length, bitRate);

            dat = Sodium.Encode(dat, Rtp.MakeNonce(rtp), Key);
            dat = Rtp.Encode(rtp, dat);

            if (SynchronizerTicks == 0)
            {
                SynchronizerTicks      = Stopwatch.GetTimestamp();
                SynchronizerResolution = (Stopwatch.Frequency * 0.02);
                TickResolution         = 10_000_000.0 / Stopwatch.Frequency;
                Discord.DebugLogger.LogMessage(LogLevel.Debug, "VoiceNext", $"Timer accuracy: {Stopwatch.Frequency.ToString("#,##0", CultureInfo.InvariantCulture)}/{SynchronizerResolution.ToString(CultureInfo.InvariantCulture)} (high resolution? {Stopwatch.IsHighResolution})", DateTime.Now);
            }
            else
            {
                // Provided by Laura#0090 (214796473689178133); this is Python, but adaptable:
                //
                // delay = max(0, self.delay + ((start_time + self.delay * loops) + - time.time()))
                //
                // self.delay
                //   sample size
                // start_time
                //   time since streaming started
                // loops
                //   number of samples sent
                // time.time()
                //   DateTime.Now

                var cts = Math.Max(Stopwatch.GetTimestamp() - SynchronizerTicks, 0);
                if (cts < SynchronizerResolution)
                {
                    await Task.Delay(TimeSpan.FromTicks((long)((SynchronizerResolution - cts) * TickResolution))).ConfigureAwait(false);
                }

                SynchronizerTicks += SynchronizerResolution;
            }

            await SendSpeakingAsync(true).ConfigureAwait(false);

            await UdpClient.SendAsync(dat, dat.Length).ConfigureAwait(false);

            Sequence++;
            Timestamp += 48 * (uint)blockSize;

            PlaybackSemaphore.Release();
        }
Exemplo n.º 20
0
        /// <summary>
        /// Load a saved import mapping for the given projects and imports the elements found into a new or existing opus.
        /// </summary>
        /// <param name="id">Project Id</param>
        /// <param name="opusId">Opus Id, if import goes to an existing opus.</param>
        /// <param name="importOverwrite">Overwrite existing data from a previous import.</param>
        /// <param name="splitOpus">Use this option to create a new opus for each import file.</param>
        /// <param name="MissedResults">Returns the number of missed styles (not mapped and hence not imported).</param>
        /// <param name="SuccessResults">Returns the number of imported styles (mapped and hence imported).</param>
        /// <returns>The project this import goes to.</returns>
        public Project SaveImportToProject(int id, int?opusId, bool importOverwrite, bool splitOpus, out List <string> MissedResults, out List <string> SuccessResults)
        {
            var prj = Ctx.Projects
                      .Include("Team.Members.Role")
                      .FirstOrDefault(p => p.Id == id);
            Opus opus;
            List <ResourceFile> docxRes;
            var importModule   = LoadImport("Default Mapping", id);
            var successResults = new List <string>();
            var missedResults  = new List <string>();

            // Attach the events to monitor the process
            importModule.GetItemFromBlobStore += new EventHandler <BlobStoreEventArgs>(importModule_GetItemFromBlobStore);
            importModule.ItemMissed           += new EventHandler <ProcessEventArgs>((o, e) => missedResults.Add(e.Name));
            importModule.ItemProcessed        += new EventHandler <ProcessEventArgs>((o, e) => successResults.Add(e.Name));
            importModule.StoreItemInBlobStore += new BlobStoreEventHandler(importModule_StoreItemInBlobStore);
            if (!splitOpus && opusId.HasValue)
            {
                opus    = Ctx.Opuses.Find(opusId);
                docxRes = Ctx.Resources.Where(r => r.Project.Id == id && (r.Parent.Name == opus.Name) && r.TypesOfResource == TypeOfResource.Import && r.Name.EndsWith(".docx")).OfType <ResourceFile>().ToList();
                DoImports(id, importModule, opus, docxRes, importOverwrite);
            }
            else
            {
                // As we're creating new Opus' we need a lead for the milestones
                var owner = prj.Team.Members.First(m => m.TeamLead);
                // get all Opus and import one by one
                foreach (var item in GetImportFolders())
                {
                    opus = Ctx.Opuses.FirstOrDefault(o => o.Name == item && o.Project.Id == id);
                    if (opus == null)
                    {
                        // opus does not exist, so we create one just right here
                        opus = new Opus {
                            Project         = prj,
                            Name            = item,
                            Active          = true,
                            Parent          = null,
                            Version         = 1,
                            PreviousVersion = null
                        };
                        var mstn = ProjectManager.Instance.CreateDefaultMileStones(owner, opus);
                        mstn.ForEach(m => Ctx.Milestones.Add(m));
                        opus.Milestones = mstn;
                        Ctx.Opuses.Add(opus);
                        Ctx.SaveChanges();
                    }
                    docxRes = Ctx.Resources.Where(r => r.Project.Id == id && (r.Parent.Name == item) && r.TypesOfResource == TypeOfResource.Import && r.Name.EndsWith(".docx")).OfType <ResourceFile>().ToList();
                    DoImports(id, importModule, opus, docxRes, importOverwrite);
                }
            }
            MissedResults  = missedResults;
            SuccessResults = successResults;
            return(prj);
        }
Exemplo n.º 21
0
        internal VoiceNextConnection(DiscordClient client, DiscordGuild guild, DiscordChannel channel, VoiceNextConfiguration config, VoiceServerUpdatePayload server, VoiceStateUpdatePayload state)
        {
            Discord = client;
            Guild   = guild;
            Channel = channel;

            TransmittingSSRCs = new ConcurrentDictionary <uint, AudioSender>();
            _userSpeaking     = new AsyncEvent <UserSpeakingEventArgs>(Discord.EventErrorHandler, "VNEXT_USER_SPEAKING");
            _userJoined       = new AsyncEvent <VoiceUserJoinEventArgs>(Discord.EventErrorHandler, "VNEXT_USER_JOINED");
            _userLeft         = new AsyncEvent <VoiceUserLeaveEventArgs>(Discord.EventErrorHandler, "VNEXT_USER_LEFT");
            _voiceReceived    = new AsyncEvent <VoiceReceiveEventArgs>(Discord.EventErrorHandler, "VNEXT_VOICE_RECEIVED");
            _voiceSocketError = new AsyncEvent <SocketErrorEventArgs>(Discord.EventErrorHandler, "VNEXT_WS_ERROR");
            TokenSource       = new CancellationTokenSource();

            Configuration = config;
            Opus          = new Opus(AudioFormat);
            //this.Sodium = new Sodium();
            Rtp = new Rtp();

            ServerData = server;
            StateData  = state;

            var eps = ServerData.Endpoint;
            var epi = eps.LastIndexOf(':');
            var eph = string.Empty;
            var epp = 80;

            if (epi != -1)
            {
                eph = eps.Substring(0, epi);
                epp = int.Parse(eps.Substring(epi + 1));
            }
            else
            {
                eph = eps;
            }
            ConnectionEndpoint = new ConnectionEndpoint {
                Hostname = eph, Port = epp
            };

            ReadyWait     = new TaskCompletionSource <bool>();
            IsInitialized = false;
            IsDisposed    = false;

            PlayingWait         = null;
            PacketQueue         = new ConcurrentQueue <VoicePacket>();
            KeepaliveTimestamps = new ConcurrentDictionary <ulong, long>();

            UdpClient                = Discord.Configuration.UdpClientFactory();
            VoiceWs                  = Discord.Configuration.WebSocketClientFactory(Discord.Configuration.Proxy);
            VoiceWs.Disconnected    += VoiceWS_SocketClosed;
            VoiceWs.MessageRecieved += VoiceWS_SocketMessage;
            VoiceWs.Connected       += VoiceWS_SocketOpened;
            VoiceWs.Errored         += VoiceWs_SocketErrored;
        }
Exemplo n.º 22
0
        /// <summary>
        /// Create deep level copy of an opus and export as plain HTML using the goven groups builder attributes.
        /// </summary>
        /// <param name="opus">Source</param>
        /// <param name="createImage">handler to store images</param>
        /// <param name="scaleImage">handler to scale/change images according to image properties</param>
        /// <param name="builderContent">Callback to get the content from caller</param>
        /// <param name="numbering">Instruction to create section numbers.</param>
        /// <param name="path">Path to savely store temporary files.</param>
        /// <param name="target">Build for HTML, RSS, PDF, and so  on.</param>
        /// <returns>HTML</returns>
        public string CreateDocumentHtml(Opus opus, CreateImageHandler createImage, ScaleImageHandler scaleImage, Func <string> builderContent, IDictionary <string, NumberingSchema> numbering, string path, GroupKind target)
        {
            opus.CreateImage += createImage;
            opus.ScaleImage  += scaleImage;
            opus.TempPath     = path;
            opus.Numbering    = numbering;
            var builder = opus.GetType().GetCustomAttributes(typeof(SnippetBuilderAttribute), true).OfType <SnippetBuilderAttribute>().Single(sb => sb.Target == target);

            opus.BuiltContent = builderContent();
            return(builder.BuildHtml(opus, numbering, null));
        }
Exemplo n.º 23
0
        /// <summary> Produces Opus encoded audio from PCM samples. </summary>
        /// <param name="input">PCM samples to encode.</param>
        /// <param name="inputOffset">Offset of the frame in pcmSamples.</param>
        /// <param name="output">Buffer to store the encoded frame.</param>
        /// <returns>Length of the frame contained in outputBuffer.</returns>
        public unsafe int EncodeFrame(byte[] input, int inputOffset, byte[] output)
        {
            int result = 0;

            fixed(byte *inPtr = input)
            result = Opus.Encode(_ptr, inPtr + inputOffset, SamplesPerFrame, output, output.Length);

            if (result < 0)
            {
                throw new Exception(((OpusError)result).ToString());
            }

            return(result);
        }
Exemplo n.º 24
0
        internal void PreparePacket(ReadOnlySpan <byte> pcm, ref Memory <byte> target)
        {
            var audioFormat = AudioFormat;

            var packetArray = ArrayPool <byte> .Shared.Rent(Rtp.CalculatePacketSize(audioFormat.SampleCountToSampleSize(audioFormat.CalculateMaximumFrameSize()), SelectedEncryptionMode));

            var packet = packetArray.AsSpan();

            Rtp.EncodeHeader(Sequence, Timestamp, SSRC, packet);
            var opus = packet.Slice(Rtp.HeaderSize, pcm.Length);

            Opus.Encode(pcm, ref opus);

            Sequence++;
            Timestamp += (uint)audioFormat.CalculateFrameSize(audioFormat.CalculateSampleDuration(pcm.Length));

            Span <byte> nonce = new byte[Sodium.NonceSize];

            switch (SelectedEncryptionMode)
            {
            case EncryptionMode.XSalsa20_Poly1305:
                Sodium.GenerateNonce(packet.Slice(0, Rtp.HeaderSize), nonce);
                break;

            case EncryptionMode.XSalsa20_Poly1305_Suffix:
                Sodium.GenerateNonce(nonce);
                break;

            case EncryptionMode.XSalsa20_Poly1305_Lite:
                Sodium.GenerateNonce(Nonce++, nonce);
                break;

            default:
                ArrayPool <byte> .Shared.Return(packetArray);

                throw new Exception("Unsupported encryption mode.");
            }

            Span <byte> encrypted = new byte[Sodium.CalculateTargetSize(opus)];

            Sodium.Encrypt(opus, encrypted, nonce);
            encrypted.CopyTo(packet.Slice(Rtp.HeaderSize));
            packet = packet.Slice(0, Rtp.CalculatePacketSize(encrypted.Length, SelectedEncryptionMode));
            Sodium.AppendNonce(nonce, packet, SelectedEncryptionMode);

            target = target.Slice(0, packet.Length);
            packet.CopyTo(target.Span);
            ArrayPool <byte> .Shared.Return(packetArray);
        }
Exemplo n.º 25
0
        /// <summary>
        /// Create deep level copy of one chapter of an opus and export as plain HTML using the goven groups builder attributes.
        /// </summary>
        /// <param name="opus">Reference to parent opus</param>
        /// <param name="chapter">Source Chapter</param>
        /// <param name="createImage">handler to store images</param>
        /// <param name="scaleImage">handler to scale/change images according to image properties</param>
        /// <param name="builderContent">Callback to get the content from caller</param>
        /// <param name="numbering">Instruction to create section numbers.</param>
        /// <param name="path">Path to savely store temporary files.</param>
        /// <param name="target">Build for HTML, RSS, PDF, and so  on.</param>
        /// <returns>HTML</returns>
        public string CreateChapterHtml(Opus document, Section chapter, CreateImageHandler createImage, ScaleImageHandler scaleImage, string path, GroupKind target, bool withNumbers = true)
        {
            if (chapter.Parent.Id != document.Id)
            {
                throw new ArgumentOutOfRangeException("chapter");
            }
            document.CreateImage += createImage;
            document.ScaleImage  += scaleImage;
            document.TempPath     = path;
            document.Numbering    = GetLocalizedNumberingSchema();
            var html = new StringBuilder();

            html.Append(CreateHtmlInner(new Snippet[] { chapter }, document.Numbering, target));
            return(html.ToString());
        }
Exemplo n.º 26
0
        public OverviewDto(string userName, Opus opus, ProjectManager projectUnitOfWork, ReaderManager readerManager) : base(userName, opus, projectUnitOfWork, readerManager)
        {
            var projId = opus.Project.Id;
            var pckgs  = projectUnitOfWork.GetAssignedMarketingPackage(projId) ?? projectUnitOfWork.GetAndAddMarketingPackage(opus.Project);

            if (opus.Published != null)
            {
                PublishingDate = opus.Published.CreatedAt;
                Keywords       = opus.Published.ExternalPublisher.Keywords.Split(';', ',');
                Categories     = opus.Published.Catalogs.Select(c => c.Name);
                License        = pckgs.GetLocalizedMarketingType();
                Language       = new CultureInfo(opus.LocaleId).NativeName;
                Location       = "Berlin"; // TODO: Manage publishers and their location
            }
        }
Exemplo n.º 27
0
        public override JsonBehavior GetJson(Opus doc, Snippet currentChapter, Snippet i)
        {
            var snippet = i as TextSnippet;

            return(new TextJsonBehavior
            {
                documentId = doc.Id,
                chapterId = currentChapter.Id,
                snippetId = snippet.Id,
                content = snippet.RawContent,
                widgetName = snippet.WidgetName,
                levelId = snippet.Level,
                parentId = snippet.Parent.Id,
                orderNr = snippet.OrderNr,
                isReadOnly = snippet.ReadOnly
            });
        }
Exemplo n.º 28
0
        public override JsonBehavior GetJson(Opus doc, Snippet currentChapter, Snippet i)
        {
            var snippet = i as SidebarSnippet;

            return(new SidebarJsonBehavior
            {
                documentId = doc.Id,
                chapterId = currentChapter.Id,
                snippetId = snippet.Id,
                content = snippet.RawContent,
                isEditableContent = (!snippet.ReadOnly && snippet.SidebarType == SidebarType.Custom),
                sidebarType = (int)snippet.SidebarType,
                asideContent = snippet.AsideContent,
                headerContent = snippet.HeaderContent,
                widgetName = snippet.WidgetName,
                levelId = snippet.Level,
                parentId = snippet.Parent.Id,
                orderNr = snippet.OrderNr,
                isReadOnly = snippet.ReadOnly
            });
        }
Exemplo n.º 29
0
    private static IEnumerator DecompressCoroutine(AudioClip clip, byte[] bytes)
    {
        int largestPacket = (bytes[8] << 8) | bytes[9];
        //Debug.Log("Largest packet: " + largestPacket);

        int opusSampleRate = GetClosestOpusSampleRate(clip.frequency);
        int error;
        IntPtr decoder = Opus.opus_decoder_create(opusSampleRate, clip.channels, out error);
        if ((Opus.Errors)error != Opus.Errors.OK)
            throw new Exception("Error creating decoder " + (Opus.Errors)error);

        int frameSize = opusSampleRate / FRAMES_PER_SECOND; // 20ms
        int blockSize = frameSize * clip.channels;
        float[] sampleBlock = new float[blockSize];
        byte[] packet = new byte[largestPacket];
        int byteI = HEADER_SIZE;
        int sampleI = 0;
        float timeDecompressed = 0;
        while (byteI < bytes.Length)
        {
            int packetSize = bytes[byteI] * 256 + bytes[byteI + 1];
            System.Buffer.BlockCopy(bytes, byteI + 2, packet, 0, packetSize);
            byteI += packetSize + 2;
            int numSamples = Opus.opus_decode_float(decoder, packet, packetSize, sampleBlock, frameSize, 0);
            if (numSamples < 0)
                throw new Exception("Decoding failed " + (Opus.Errors)numSamples);
            clip.SetData(sampleBlock, sampleI);
            sampleI += numSamples;
            timeDecompressed += numSamples / (float)clip.frequency;
            if (timeDecompressed >= Time.deltaTime * 2)
            {
                // decompress twice as fast
                yield return null;
                timeDecompressed = 0;
            }
        }
        Opus.opus_decoder_destroy(decoder);
        //Debug.Log("Completely decompressed!");
    }
Exemplo n.º 30
0
// ReSharper restore InconsistentNaming

        #endregion

        #region Override, Properties

        #endregion

        #region Overriden Methods

        public override JsonBehavior GetJson(Opus doc, Snippet currentChapter, Snippet i)
        {
            var snippet = i as ListingSnippet;

            return(new ListingJsonBehavior
            {
                documentId = doc.Id,
                chapterId = currentChapter.Id,
                snippetId = snippet.Id,
                listingLocalization = "Listing",
                syntaxHighlight = snippet.SyntaxHighlight,
                genericChapterNumber = doc.ShowNumberChain ? currentChapter.OrderNr.ToString() : "#",
                lineNumbers = snippet.LineNumbers,
                content = snippet.RawContent,
                widgetName = snippet.WidgetName,
                levelId = snippet.Level,
                parentId = snippet.Parent.Id,
                orderNr = snippet.OrderNr,
                snippetCounter = currentChapter.Children.FlattenHierarchy().OfType <ListingSnippet>().ToList().IndexOf(snippet) + 1,
                language = snippet.Language,
                isReadOnly = snippet.ReadOnly,
                title = snippet.Title
            });
        }
Exemplo n.º 31
0
        /// <summary>
        ///  This method will return a section snippet, with all desired attributes
        /// </summary>
        /// <param name="documentId">Current document id</param>
        /// <param name="chapterId">Current chapter id</param>
        /// <param name="i">This variable respresent to the section</param>
        /// <returns>Section json attributes</returns>
        public JsonBehavior GetJson(Opus doc, Snippet currentChapter, Section currentElement, string numberChain)
        {
            var sectionNumberChain = string.Empty;

            if (currentElement.Parent.Id != doc.Id)
            {
                sectionNumberChain = numberChain;
            }
            return(new SectionJsonBehavior {
                documentId = doc.Id,
                chapterId = currentChapter.Id,
                snippetId = currentElement.Id,
                content = currentElement.RawContent,
                widgetName = currentElement.WidgetName,
                sectionNumberChain = sectionNumberChain,
                genericChapterNumber = doc.ShowNumberChain ? currentChapter.OrderNr.ToString() : "#",
                levelId = currentElement.Level,
                parentId = currentElement.Parent.Id,
                orderNr = currentElement.OrderNr,
                childCount = (currentElement.HasChildren() ? currentElement.Children.Count() : 0),
                hasChildren = currentElement.HasChildren(),
                isReadOnly = currentElement.ReadOnly
            });
        }
Exemplo n.º 32
0
        private bool ProcessPacket(ReadOnlySpan <byte> data, ref Memory <byte> opus, ref Memory <byte> pcm, IList <ReadOnlyMemory <byte> > pcmPackets, out AudioSender voiceSender, out AudioFormat outputFormat)
        {
            voiceSender  = null;
            outputFormat = default;

            if (!this.Rtp.IsRtpHeader(data))
            {
                return(false);
            }

            this.Rtp.DecodeHeader(data, out var sequence, out var timestamp, out var ssrc, out var hasExtension);

            if (!this.TransmittingSSRCs.TryGetValue(ssrc, out var vtx))
            {
                var decoder = Opus.CreateDecoder();

                vtx = new AudioSender(ssrc, decoder)
                {
                    // user isn't present as we haven't received a speaking event yet.
                    User = null
                };
            }

            voiceSender = vtx;
            if (sequence <= vtx.LastSequence) // out-of-order packet; discard
            {
                return(false);
            }
            var gap = vtx.LastSequence != 0 ? sequence - 1 - vtx.LastSequence : 0;

            if (gap >= 5)
            {
                this.Discord.Logger.LogWarning(VoiceNextEvents.VoiceReceiveFailure, "5 or more voice packets were dropped when receiving");
            }

            Span <byte> nonce = stackalloc byte[Sodium.NonceSize];

            this.Sodium.GetNonce(data, nonce, this.SelectedEncryptionMode);
            this.Rtp.GetDataFromPacket(data, out var encryptedOpus, this.SelectedEncryptionMode);

            var opusSize = Sodium.CalculateSourceSize(encryptedOpus);

            opus = opus.Slice(0, opusSize);
            var opusSpan = opus.Span;

            try
            {
                this.Sodium.Decrypt(encryptedOpus, opusSpan, nonce);

                // Strip extensions, if any
                if (hasExtension)
                {
                    // RFC 5285, 4.2 One-Byte header
                    // http://www.rfcreader.com/#rfc5285_line186
                    if (opusSpan[0] == 0xBE && opusSpan[1] == 0xDE)
                    {
                        var headerLen = opusSpan[2] << 8 | opusSpan[3];
                        var i         = 4;
                        for (; i < headerLen + 4; i++)
                        {
                            var @byte = opusSpan[i];

                            // ID is currently unused since we skip it anyway
                            //var id = (byte)(@byte >> 4);
                            var length = (byte)(@byte & 0x0F) + 1;

                            i += length;
                        }

                        // Strip extension padding too
                        while (opusSpan[i] == 0)
                        {
                            i++;
                        }

                        opusSpan = opusSpan.Slice(i);
                    }

                    // TODO: consider implementing RFC 5285, 4.3. Two-Byte Header
                }

                if (opusSpan[0] == 0x90)
                {
                    // I'm not 100% sure what this header is/does, however removing the data causes no
                    // real issues, and has the added benefit of removing a lot of noise.
                    opusSpan = opusSpan.Slice(2);
                }

                if (gap == 1)
                {
                    var lastSampleCount = this.Opus.GetLastPacketSampleCount(vtx.Decoder);
                    var fecpcm          = new byte[this.AudioFormat.SampleCountToSampleSize(lastSampleCount)];
                    var fecpcmMem       = fecpcm.AsSpan();
                    this.Opus.Decode(vtx.Decoder, opusSpan, ref fecpcmMem, true, out _);
                    pcmPackets.Add(fecpcm.AsMemory(0, fecpcmMem.Length));
                }
                else if (gap > 1)
                {
                    var lastSampleCount = this.Opus.GetLastPacketSampleCount(vtx.Decoder);
                    for (var i = 0; i < gap; i++)
                    {
                        var fecpcm    = new byte[this.AudioFormat.SampleCountToSampleSize(lastSampleCount)];
                        var fecpcmMem = fecpcm.AsSpan();
                        this.Opus.ProcessPacketLoss(vtx.Decoder, lastSampleCount, ref fecpcmMem);
                        pcmPackets.Add(fecpcm.AsMemory(0, fecpcmMem.Length));
                    }
                }

                var pcmSpan = pcm.Span;
                this.Opus.Decode(vtx.Decoder, opusSpan, ref pcmSpan, false, out outputFormat);
                pcm = pcm.Slice(0, pcmSpan.Length);
            }
            finally
            {
                vtx.LastSequence = sequence;
            }

            return(true);
        }
Exemplo n.º 33
0
 void ConsoleTest_UpdateOptions(Opus.Core.IModule module, Opus.Core.Target target)
 {
     C.ILinkerOptions linkerOptions = module.Options as C.ILinkerOptions;
     linkerOptions.DoNotAutoIncludeStandardLibraries = false;
 }
Exemplo n.º 34
0
        public void OverrideOptions(Opus.Core.BaseOptionCollection optionCollection, Opus.Core.Target target)
        {
            VisualCCommon.ICCompilerOptions vcCompilerInterface = optionCollection as VisualCCommon.ICCompilerOptions;
            if (null != vcCompilerInterface)
            {
                //vcCompilerInterface.BasicRuntimeChecks = VisualCCommon.EBasicRuntimeChecks.StackFrameAndUninitializedVariables;
                vcCompilerInterface.BasicRuntimeChecks = VisualCCommon.EBasicRuntimeChecks.None;
            }

            C.LinkerOptionCollection linkOptions = optionCollection as C.LinkerOptionCollection;
            if (null != linkOptions)
            {
                linkOptions.OutputDirectoryPath = System.IO.Path.Combine(Opus.Core.State.BuildRoot, System.IO.Path.Combine(target.Key, "bin"));
            }
        }
Exemplo n.º 35
0
            void SourceFiles_UpdateOptions2(Opus.Core.IModule module, Opus.Core.Target target)
            {
                C.ICCompilerOptions compilerOptions = module.Options as C.ICCompilerOptions;

                C.ICxxCompilerOptions cxxCompilerOptions = module.Options as C.ICxxCompilerOptions;
                cxxCompilerOptions.ExceptionHandler = C.Cxx.EExceptionHandler.Asynchronous;

                #if true
                #else
                C.IToolchainOptions toolchainOptions = compilerOptions.ToolchainOptionCollection as C.IToolchainOptions;
                toolchainOptions.CharacterSet = C.ECharacterSet.Unicode;
                #endif
                // TODO: not sure why these are not passed through
                if (Opus.Core.State.PackageInfo["VisualC"].Version == "10.0")
                {
                    compilerOptions.Defines.Add("UNICODE");
                    compilerOptions.Defines.Add("_UNICODE");
                }
            }
Exemplo n.º 36
0
 void SourceFiles_UpdateOptions(Opus.Core.IModule module, Opus.Core.Target target)
 {
     C.ICCompilerOptions compilerOptions = module.Options as C.ICCompilerOptions;
     compilerOptions.IncludePaths.Include(this, "src", "src");
 }