Reactive Summit, Montreal QC, 2018 October 24 Making Music … · 2018-10-30 · A Quick Review Of...

21
Making Music With Akka Streams Reactive Summit, Montreal QC, 2018 October 24

Transcript of Reactive Summit, Montreal QC, 2018 October 24 Making Music … · 2018-10-30 · A Quick Review Of...

Page 1: Reactive Summit, Montreal QC, 2018 October 24 Making Music … · 2018-10-30 · A Quick Review Of Akka Streams Streams support continuous processing of data in a pipeline Akka Streams

Making Music With Akka StreamsReactive Summit, Montreal QC, 2018 October 24

Page 2: Reactive Summit, Montreal QC, 2018 October 24 Making Music … · 2018-10-30 · A Quick Review Of Akka Streams Streams support continuous processing of data in a pipeline Akka Streams

Agenda● Quick overview of Akka Streams● A Graph of Streams For Making Music● Quick Demonstration● Stream Construction Techniques● But Wait! There’s more.

© 2018 Yoppworks Inc. All Rights Reserved Page 2

Page 3: Reactive Summit, Montreal QC, 2018 October 24 Making Music … · 2018-10-30 · A Quick Review Of Akka Streams Streams support continuous processing of data in a pipeline Akka Streams

A Quick Review Of Akka Streams● Streams support continuous processing of data in a pipeline● Akka Streams provides a rich set of operators to compose streams● Build a model of the stream and then run it as many times as needed.● Three Element Kinds For Modeling a Stream: Source, Flow, Sink

© 2018 Yoppworks Inc. All Rights Reserved Page 3

Page 4: Reactive Summit, Montreal QC, 2018 October 24 Making Music … · 2018-10-30 · A Quick Review Of Akka Streams Streams support continuous processing of data in a pipeline Akka Streams

Music Maker Streams Graph

© 2018 Yoppworks Inc. All Rights Reserved Page 4

Mouse Events

Keyboard Events

pitches

volumes

onOffs

portamentos

instruments

Window Events

keys

pitchesAndVolumes

speciaKeys

MusicMaker

Message Merge Hub

Mouse Y

Mouse X

Buttons

Wheel

Enter

Exit

Normal

Control PlayNote

PlayNote

PlayNote

SetPitchBend

ChangeInstrument

IgnoreNotes

ResumeNotes

MusicMaker

MusicMaker

Messages

ScreenOutput

MusicMaker

Messages

Page 5: Reactive Summit, Montreal QC, 2018 October 24 Making Music … · 2018-10-30 · A Quick Review Of Akka Streams Streams support continuous processing of data in a pipeline Akka Streams

Quick Demonstration

5

Page 6: Reactive Summit, Montreal QC, 2018 October 24 Making Music … · 2018-10-30 · A Quick Review Of Akka Streams Streams support continuous processing of data in a pipeline Akka Streams

Stream Construction Techniques

6

Page 7: Reactive Summit, Montreal QC, 2018 October 24 Making Music … · 2018-10-30 · A Quick Review Of Akka Streams Streams support continuous processing of data in a pipeline Akka Streams

Creating A Source With An Actor

© 2018 Yoppworks Inc. All Rights Reserved Page 7

● Messages sent to an Actor become elements of the stream● Must pay attention to buffering and overflow● Back pressure is not possible with this kind of Source● Source.actorRef materializes the Actor and the Source● Messages sent to the Actor become elements in the Source● This is how we transform Swing events to stream elements

Page 8: Reactive Summit, Montreal QC, 2018 October 24 Making Music … · 2018-10-30 · A Quick Review Of Akka Streams Streams support continuous processing of data in a pipeline Akka Streams

Extracting Elements By Type

● Source.collecType[T] can be used to filter out all messages except those of a specific type.

● Shown above is how we get a source of MouseWheelRotated messages from a source of many types of events.

● Shown below is how we get a source of MouseClicked messages from a source of many types of events.

● Note the use of classTag[MouseClicked] from scala.reflect.

© 2018 Yoppworks Inc. All Rights Reserved Page 8

Page 9: Reactive Summit, Montreal QC, 2018 October 24 Making Music … · 2018-10-30 · A Quick Review Of Akka Streams Streams support continuous processing of data in a pipeline Akka Streams

Generating Elements At A Fixed Rate● Use Source.tick to get a stream of elements at a fixed rate● Source.tick has three arguments: initialDelay, interval, and tick● Map the “tick” to the desired type● Here we’ve mapped a Unit value, ( ), to MouseRelocated with data that comes

from the MouseInfo.getPointerInfo.getLocation function from Swing

© 2018 Yoppworks Inc. All Rights Reserved Page 9

Page 10: Reactive Summit, Montreal QC, 2018 October 24 Making Music … · 2018-10-30 · A Quick Review Of Akka Streams Streams support continuous processing of data in a pipeline Akka Streams

Transforming Elements With map● Here we convert a MouseWheelRotated swing event into a simple Int

© 2018 Yoppworks Inc. All Rights Reserved Page 10

Page 11: Reactive Summit, Montreal QC, 2018 October 24 Making Music … · 2018-10-30 · A Quick Review Of Akka Streams Streams support continuous processing of data in a pipeline Akka Streams

© 2018 Yoppworks Inc. All Rights Reserved Page 11

Transformation Of Elements● Update paneData and convert to a pair of MusicMakerMessages

Page 12: Reactive Summit, Montreal QC, 2018 October 24 Making Music … · 2018-10-30 · A Quick Review Of Akka Streams Streams support continuous processing of data in a pipeline Akka Streams

Merge And Broadcast

© 2018 Yoppworks Inc. All Rights Reserved Page 12

● Merge a group of Sources together● Materialize it with BroadcastHub.sink to obtain

RunnableGraph[Source[MusicMakerMessage,NotUse]]

● Use the RunnableGraph to get multiple Sources for the broadcast.

Page 13: Reactive Summit, Montreal QC, 2018 October 24 Making Music … · 2018-10-30 · A Quick Review Of Akka Streams Streams support continuous processing of data in a pipeline Akka Streams

Combining Two Sources With A Graph

© 2018 Yoppworks Inc. All Rights Reserved Page 13

● This defines a Flow● The logic retains the

value of volumes in lastVolume.

● Elements flow on the timing of the pitches (input)

● Output is PlayNote

Page 14: Reactive Summit, Montreal QC, 2018 October 24 Making Music … · 2018-10-30 · A Quick Review Of Akka Streams Streams support continuous processing of data in a pipeline Akka Streams

© 2018 Yoppworks Inc. All Rights Reserved Page 14

Random Music Generation

3 Sources combined to set the instrument, play the notes and stop the channel.

Note use of limit(n)

Velocity (volume) is limited to a quieter but audible range.

Page 15: Reactive Summit, Montreal QC, 2018 October 24 Making Music … · 2018-10-30 · A Quick Review Of Akka Streams Streams support continuous processing of data in a pipeline Akka Streams

DemonstrateAll Features

15

Page 16: Reactive Summit, Montreal QC, 2018 October 24 Making Music … · 2018-10-30 · A Quick Review Of Akka Streams Streams support continuous processing of data in a pipeline Akka Streams

But wait! There’s Prometheus Too!

© 2018 Yoppworks Inc. All Rights Reserved Page 16

MusicMaker

Message Merge Hub

MusicMaker

MusicMaker

Messages

ScreenOutputMusic

MakerMessages

/metrics

MusicMaker

Messages

Music Inputs

Page 17: Reactive Summit, Montreal QC, 2018 October 24 Making Music … · 2018-10-30 · A Quick Review Of Akka Streams Streams support continuous processing of data in a pipeline Akka Streams

Demonstrate Grafana/Prometheus

17

Page 18: Reactive Summit, Montreal QC, 2018 October 24 Making Music … · 2018-10-30 · A Quick Review Of Akka Streams Streams support continuous processing of data in a pipeline Akka Streams

Diverting Messages To Metrics Server

© 2018 Yoppworks Inc. All Rights Reserved Page 18

● Use “via” to process data through a Flow (next page)● Sample and print every 1000th message.

Page 19: Reactive Summit, Montreal QC, 2018 October 24 Making Music … · 2018-10-30 · A Quick Review Of Akka Streams Streams support continuous processing of data in a pipeline Akka Streams

Making A Flow From A FunctionUse:

Flow.fromFunction

to do intermediate processing in a stream.

Each of the purple values is a metric defined with the Prometheus Java Client. This is how the metrics are collected.

© 2018 Yoppworks Inc. All Rights Reserved Page 19

Page 20: Reactive Summit, Montreal QC, 2018 October 24 Making Music … · 2018-10-30 · A Quick Review Of Akka Streams Streams support continuous processing of data in a pipeline Akka Streams

And It Runs On Raspberry Pi 3+(Demonstration)

20

Page 21: Reactive Summit, Montreal QC, 2018 October 24 Making Music … · 2018-10-30 · A Quick Review Of Akka Streams Streams support continuous processing of data in a pipeline Akka Streams

ThankYou!

● About YoppWorks Inc:○ Lightbend Platinum Partner○ We Deliver Services & Products○ Based In Ottawa, Canada○ Lightbend Training, Consulting

GoSpaces, RobotHive, Fast Data

● Presentation Code Is Here:○ https://github.com/YoppWorks/mmaws.git

21

[email protected]