@babblevoice/projectrtp 2.5.34 → 2.5.38

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -27,6 +27,22 @@ function gensignal( hz, datalength, magnitude ) {
27
27
  return y
28
28
  }
29
29
 
30
+ function concatint16arrays( arrays ) {
31
+ const totalLength = arrays.reduce((sum, arr) => sum + arr.length, 0)
32
+
33
+ // Step 2: Create a new Int16Array with total length
34
+ const result = new Int16Array(totalLength)
35
+
36
+ // Step 3: Copy each Int16Array into the result
37
+ let offset = 0
38
+ for (const arr of arrays) {
39
+ result.set(arr, offset)
40
+ offset += arr.length
41
+ }
42
+
43
+ return result
44
+ }
45
+
30
46
  /**
31
47
  *
32
48
  * @param { Array< number > } inarray
@@ -41,6 +57,20 @@ function pcmutolinear( inarray ) {
41
57
  return out
42
58
  }
43
59
 
60
+ /**
61
+ *
62
+ * @param { Array< number > } inarray
63
+ * @returns { Int16Array }
64
+ */
65
+ function pcmatolinear( inarray ) {
66
+ const out = new Int16Array( inarray.length )
67
+ for( let i = 0; i < inarray.length; i++ ) {
68
+ out[ i ] = prtp.projectrtp.codecx.pcma2linear16( inarray[ i ] )
69
+ }
70
+
71
+ return out
72
+ }
73
+
44
74
  /**
45
75
  * Limitation of not parsing ccrc.
46
76
  * @param { Buffer } packet
@@ -403,6 +433,83 @@ describe( "rtpsound", function() {
403
433
  expect( receviedpkcount ).to.be.above( 400 )
404
434
  } )
405
435
 
436
+ it( "play uk ringing and check duration", async function() {
437
+ /* create our RTP/UDP endpoint */
438
+ const server = dgram.createSocket( "udp4" )
439
+
440
+ /** @type { prtp.channel } */
441
+ let channel
442
+
443
+ let receivedpcmu = []
444
+ server.on( "message", function( msg ) {
445
+ receivedpcmu.push( pcmatolinear( rtputils.parsepk( msg ).payload ) )
446
+ } )
447
+
448
+ this.timeout( 6000 )
449
+ this.slow( 5000 )
450
+
451
+ let done
452
+ const finished = new Promise( ( r ) => { done = r } )
453
+
454
+ server.bind()
455
+ await new Promise( resolve => server.on( "listening", resolve ) )
456
+
457
+ const ourport = server.address().port
458
+
459
+ let starttime = 0, endtime = 0
460
+ let playcount = 0
461
+ channel = await prtp.projectrtp.openchannel( { "remote": { "address": "localhost", "port": ourport, "codec": 8 } }, function( d ) {
462
+ //console.log(d)
463
+ if( "play" === d.action && "start" == d.event ) {
464
+ playcount++
465
+ if( playcount >= 1 )
466
+ starttime = + new Date()
467
+ }
468
+ if( "play" === d.action && "end" == d.event && "completed" == d.reason ) {
469
+ endtime = + new Date()
470
+ done()
471
+ }
472
+ } )
473
+
474
+ const justringing = { "loop": false, "files": [
475
+ { "wav": "/tmp/uksounds.wav", "start": 1000, "stop": 4000 }
476
+ ] }
477
+
478
+ const flatandringing = { "loop": false, "files": [
479
+ { "wav": "/tmp/flat2.wav" },
480
+ { "wav": "/tmp/uksounds.wav", "start": 1000, "stop": 4000 }
481
+ ] }
482
+
483
+ expect( channel.play( justringing ) ).to.be.true
484
+ expect( channel.play( flatandringing ) ).to.be.true
485
+
486
+ await finished
487
+
488
+ channel.close()
489
+ server.close()
490
+
491
+ await new Promise( resolve => setTimeout( resolve, 100 ) )
492
+
493
+ const receviedaudio = concatint16arrays( receivedpcmu )
494
+
495
+ /* further test - if you want to play the output */
496
+ if( false ) {
497
+
498
+ console.log( receviedaudio )
499
+
500
+ Buffer.from( receviedaudio )
501
+
502
+ const filedata = Buffer.concat( [ createwavheader( receviedaudio.length ), Buffer.from( receviedaudio.buffer ) ] )
503
+ await fs.promises.writeFile("/tmp/testout.wav", filedata )
504
+ }
505
+
506
+ //console.log( endtime - starttime )
507
+ expect( receviedaudio.length ).to.be.greaterThan( 23000 )
508
+ expect( endtime - starttime ).to.be.greaterThan( 4000 )
509
+ expect( endtime - starttime ).to.be.lessThan( 4200 )
510
+
511
+ } )
512
+
406
513
  before( async () => {
407
514
 
408
515
  const samples = 8000
@@ -432,6 +539,18 @@ describe( "rtpsound", function() {
432
539
  const sig = gensignal( 440, 8000, 1000 )
433
540
  await fs.promises.writeFile( "/tmp/440sine.wav", Buffer.concat( [ wavheader, sig ] ) )
434
541
 
542
+ const uktones = "/tmp/uksounds.wav"
543
+ prtp.projectrtp.tone.generate( "350+440*0.5:1000", uktones )
544
+ prtp.projectrtp.tone.generate( "400+450*0.5/0/400+450*0.5/0:400/200/400/2000", uktones )
545
+ prtp.projectrtp.tone.generate( "697+1209*0.5/0/697+1336*0.5/0/697+1477*0.5/0/697+1633*0.5/0:400/100", uktones )
546
+ prtp.projectrtp.tone.generate( "770+1209*0.5/0/770+1336*0.5/0/770+1477*0.5/0/770+1633*0.5/0:400/100", uktones )
547
+ prtp.projectrtp.tone.generate( "852+1209*0.5/0/852+1336*0.5/0/852+1477*0.5/0/852+1633*0.5/0:400/100", uktones )
548
+ prtp.projectrtp.tone.generate( "941+1209*0.5/0/941+1336*0.5/0/941+1477*0.5/0/941+1633*0.5/0:400/100", uktones )
549
+ prtp.projectrtp.tone.generate( "440*0.5:1000", uktones )
550
+ prtp.projectrtp.tone.generate( "440/0:375/375", uktones )
551
+ prtp.projectrtp.tone.generate( "440/0:400/350/225/525", uktones )
552
+ prtp.projectrtp.tone.generate( "440/0:125/125", uktones )
553
+
435
554
  } )
436
555
 
437
556
  after( async () => {
@@ -439,5 +558,7 @@ describe( "rtpsound", function() {
439
558
  await fs.promises.unlink( "/tmp/flat2.wav" )
440
559
  await fs.promises.unlink( "/tmp/flat3.wav" )
441
560
  await fs.promises.unlink( "/tmp/440sine.wav" )
561
+
562
+ await fs.promises.unlink( "/tmp/uksounds.wav" )
442
563
  } )
443
564
  } )